How can I convert string into table?

I have loaded a text file that has the following text:

list={1,3,5,7}

Once I load this string, how can I actually convert it into a list?

I have tried the loadsave.lua approach, but it seems only to decode JSON data. I want to simply convert this string to a table.

Can you store your text file as a JSON file? Will make things a lot easier. Otherwise you’re going to have to split the string based on commas, ick: 

https://www.google.com/search?q=split+string+commas+site%3Acoronalabs.com

Hi Troy,

Will this string always (or usually) be in this predictable format of simply numbers? If so, I suggest using “string.match()” and “string.gmatch()” and setting up the proper pattern iterator. Actually, you could do it even if the string “table items” were non-numeric, but you’d have to include other character types in the string matching iterator as well.
 

https://docs.coronalabs.com/api/library/string/match.html

https://docs.coronalabs.com/api/library/string/gmatch.html

This kind of thing would do the trick:

[lua]

local myString = “list={1,3,5,7}”

local tableName, tablePortion = string.match( myString, “(%w+)={([%w%d%p]-)}” )

print( tableName, tablePortion )

local myTable = {}

for tableValue in string.gmatch( tablePortion, “([%w%d%.]+)” ) do

   print( tableValue )

   myTable[#myTable+1] = tableValue

end

[/lua]

Brent

My recommendation would be to start with JSON as the data format, which isn’t terribly different. But you can also run a quick file creator. Go ahead and create the table in Lua:

local list = { 1, 2, 3, 4 }

use loadSave and save list out at which point you will have a json table. Then you can use loadSave to read it and turn it back into a table. As it is now, you would need to read the file in and parse the data  yourself.

Rob

Can you store your text file as a JSON file? Will make things a lot easier. Otherwise you’re going to have to split the string based on commas, ick: 

https://www.google.com/search?q=split+string+commas+site%3Acoronalabs.com

Hi Troy,

Will this string always (or usually) be in this predictable format of simply numbers? If so, I suggest using “string.match()” and “string.gmatch()” and setting up the proper pattern iterator. Actually, you could do it even if the string “table items” were non-numeric, but you’d have to include other character types in the string matching iterator as well.
 

https://docs.coronalabs.com/api/library/string/match.html

https://docs.coronalabs.com/api/library/string/gmatch.html

This kind of thing would do the trick:

[lua]

local myString = “list={1,3,5,7}”

local tableName, tablePortion = string.match( myString, “(%w+)={([%w%d%p]-)}” )

print( tableName, tablePortion )

local myTable = {}

for tableValue in string.gmatch( tablePortion, “([%w%d%.]+)” ) do

   print( tableValue )

   myTable[#myTable+1] = tableValue

end

[/lua]

Brent

My recommendation would be to start with JSON as the data format, which isn’t terribly different. But you can also run a quick file creator. Go ahead and create the table in Lua:

local list = { 1, 2, 3, 4 }

use loadSave and save list out at which point you will have a json table. Then you can use loadSave to read it and turn it back into a table. As it is now, you would need to read the file in and parse the data  yourself.

Rob