Convert string "{1,2,3,4}" into a table {1,2,3,4}

Just want to remove the quotes and make a table out of a string.  Can’t seem to find a way to do it. Probably missing something.

Thanks,

–Scot

The string.match() API should get you going:

t = {}

t[1], t[2], t[3], t[4] = string.match("{1,2,3,4}", “{%d+,%d+,%d+,%d+}”)

or something like that.  You might need to adjust for spacing, more than 4 numbers, etc.

Rob

could you remove the braces then use json?

I think the string.split function is good to have in your arsenal:

[lua]-- From http://lua-users.org/wiki/SplitJoin

function string:split(sep)

    local sep, fields = sep or “:”, {}

    local pattern = string.format("([^%s]+)", sep)

    self:gsub(pattern, function© fields[#fields+1] = c end)

    return fields

end

[/lua]

Then you can replace the ‘{’ and ‘}’ and use the split function.

[lua]

local test = “{1,2,3,4}”

test = string.gsub (test, “[{}]”, “”), test

local testAsTable = test.split(test, ‘,’)

[/lua]

The numbers inside the table are still strings though… 

the json lib is what you’re looking for.

-- array tbl = json.decode( "[1,2,3,4]" ) for i, v in ipairs( tbl ) do print( i, v ) end -- table advancedTbl = json.decode( '{ "foo":"bar", "bar":"foo", "foobar": ["foo", "bar"], "barfoo": ["bar", "foo"] }' ) for k, v in pairs( advancedTbl ) do print( k, v ) -- prints all the keys and values in the parsed table end -- you can also access the keys directly print( advancedTbl.foobar[1] ) -- prints "foo" print( advancedTbl.barfoo[1] ) -- prints "bar" -- and you can also encode a table into a string if you need to str = json.encode( { foo = "bar", bar = "foo", foobar = { "foo", "bar" } barfoo = { "bar", "foo" } } )

Thank you for your reply.  We abandoned our last implementation of this architecture in part because json created too much overhead.  We just need to split the string in a regex sort of way without creating objects and such.

I’m nervous about using a for loop as well, though my experience so far has been that it works pretty quickly in Corona SDK.

In a while, I’ll be releasing a new version of Dusk that includes a Lua table notation parser; it’ll be some time, though.

In the meantime, your best bet is probably a serializer like Cyan or JSON. Splitting the table with Lua patterns will be much slower than either of the twain; Cyan uses LPeg and JSON is native-level code.

  • C

I can format the table inside the string like “{.2, .4, .8}”  All I really need is for Lua to strip off the quotes and use the table underneath.  I realize this is not an accepted practice because anything could be put inside the string–the same reason eval() is discouraged in Javascript.

So I guess I’ll have to write my own parser.  The performance on the device side is less important.  The server is monitoring multiple joints on up to 30 robots so it will be sending strings (or binary if available) without any JSON encoding.  This can be lossy so we aren’t even worried about ACK.

The string.match() API should get you going:

t = {}

t[1], t[2], t[3], t[4] = string.match("{1,2,3,4}", “{%d+,%d+,%d+,%d+}”)

or something like that.  You might need to adjust for spacing, more than 4 numbers, etc.

Rob

could you remove the braces then use json?

I think the string.split function is good to have in your arsenal:

[lua]-- From http://lua-users.org/wiki/SplitJoin

function string:split(sep)

    local sep, fields = sep or “:”, {}

    local pattern = string.format("([^%s]+)", sep)

    self:gsub(pattern, function© fields[#fields+1] = c end)

    return fields

end

[/lua]

Then you can replace the ‘{’ and ‘}’ and use the split function.

[lua]

local test = “{1,2,3,4}”

test = string.gsub (test, “[{}]”, “”), test

local testAsTable = test.split(test, ‘,’)

[/lua]

The numbers inside the table are still strings though… 

the json lib is what you’re looking for.

-- array tbl = json.decode( "[1,2,3,4]" ) for i, v in ipairs( tbl ) do print( i, v ) end -- table advancedTbl = json.decode( '{ "foo":"bar", "bar":"foo", "foobar": ["foo", "bar"], "barfoo": ["bar", "foo"] }' ) for k, v in pairs( advancedTbl ) do print( k, v ) -- prints all the keys and values in the parsed table end -- you can also access the keys directly print( advancedTbl.foobar[1] ) -- prints "foo" print( advancedTbl.barfoo[1] ) -- prints "bar" -- and you can also encode a table into a string if you need to str = json.encode( { foo = "bar", bar = "foo", foobar = { "foo", "bar" } barfoo = { "bar", "foo" } } )

Thank you for your reply.  We abandoned our last implementation of this architecture in part because json created too much overhead.  We just need to split the string in a regex sort of way without creating objects and such.

I’m nervous about using a for loop as well, though my experience so far has been that it works pretty quickly in Corona SDK.

In a while, I’ll be releasing a new version of Dusk that includes a Lua table notation parser; it’ll be some time, though.

In the meantime, your best bet is probably a serializer like Cyan or JSON. Splitting the table with Lua patterns will be much slower than either of the twain; Cyan uses LPeg and JSON is native-level code.

  • C

I can format the table inside the string like “{.2, .4, .8}”  All I really need is for Lua to strip off the quotes and use the table underneath.  I realize this is not an accepted practice because anything could be put inside the string–the same reason eval() is discouraged in Javascript.

So I guess I’ll have to write my own parser.  The performance on the device side is less important.  The server is monitoring multiple joints on up to 30 robots so it will be sending strings (or binary if available) without any JSON encoding.  This can be lossy so we aren’t even worried about ACK.