How To Convert A String To Table, Or How To Test For A String?

Is there a way of converting a string to a table? or testing a variable passed to a function to see if it a string?

I have an App where I want provide a choice of themes, some are pre-defined and included in the source (tables) others will be downloaded as files in the future so my function needs to the following in psuedo code…

function loadTheme(themeName)

if themeName is a string then

load theme from directory – themeName is "newTheme.json’ or similar

else

theme = themeName --this is a table  so point the global theme to it…

end

I’ve tried testing using the string functions but it gives an error if a table is passed

Help appreciated

PS has Gooogle search been turned off for Corona docs? I’m find it much harder to find anything this last week or so…

Hi there,

Yup, lua has a built in function called type to tell you the type of a variable at runtime.  The code [lua]if type(themeName) == “string” then[/lua] will tell you whether themeName is a string.

  • Andrew

As for your downloaded string, consider using JSON.  You download a json string then do:

someTable = json.decode(mydownloadedjsonstring)

 

and you end up with a table.

thanks for the suggestions the type function will help as a work around if I use a table of strings to tables to convert

I also thought about JSON, however its still comes out as a string with my code below

local themeGreen2 = { 100,101,102,103}    

    

function testfunction2()    

    inputStr = “themeGreen2”                        --want to make this point to equivalent table eg themeGreen2 defined above    

–    local str = ‘{ “theme” : ‘…"’"…inputStr…"’}" 

    local str = ‘{“theme”:“themeGreen2” }’            --debug version to test string concat above

    local tab = json.decode(str)

    if type(tab.theme) == “string” then

        print (“XX its a string”, tab.theme)

    elseif     type(tab.theme) == “table” then

        print (“XX its a table”)

        print (“theme table data”, tab.theme[1])

        

    end        

end    

Firstly I’m having issues concating a varible into a string with all the quotes required for JSON see commented out line, the second variable I’ve had to use single quotes.

However with the test string ‘{“theme”:“themeGreen2” }’ the result comes out as a string…

Rob can you shed anymore light on this for me? much appreciated…

Hi there,

For your concatenation code, try the following instead.  (I’m using Lua’s double-bracket notation for strings here, which I think makes it easier in this case.)

[lua]

local str = [[{“theme”:"]] … inputStr … [["}]]

[/lua]

For your second question, tab.theme is indeed a string, namely the string “themeGreen2”, since that’s what it is in the JSON table.  If you want to take that string and “interpret” it as the name of a variable, you could put your themes into a table and then use tab.theme as a key.  More specifically, you coudl change your first line to [lua]local themes = {themeGreen2 = {100,101,102,103}}[/lua], and then you could get the theme by doing [lua]themes[tab.theme][/lua].

  • Andrew

Hi Andrew thanks for your comments:

The Double square brackets are certainly easier to play with, however as you say it still returns a string because it is a string!

Not really thought about your idea for the rest, but it is a good one :slight_smile:

Right now I’m using a table of strings and a table of tables without using JSON, I search for a matching string and then use the same index to find the right / matching theme table, if it can’t find it uses the default.

I wanted to shy away from creating a ‘table of a table’ but in reality its little work to add an entry in the table every time I create a new theme.

Just a pity there’s no easy way for a string to be cast into variable of the same name. However the type() test is very useful

I’m not sure if this is quite what you’re looking for, but I made a library that’ll convert strings to tables and vice-versa (like json.decode and json.encode).

http://www.gymbyl.com/programming/s2t.html

It won’t work with nested tables, though.

C

Hi there,

Yup, lua has a built in function called type to tell you the type of a variable at runtime.  The code [lua]if type(themeName) == “string” then[/lua] will tell you whether themeName is a string.

  • Andrew

As for your downloaded string, consider using JSON.  You download a json string then do:

someTable = json.decode(mydownloadedjsonstring)

 

and you end up with a table.

thanks for the suggestions the type function will help as a work around if I use a table of strings to tables to convert

I also thought about JSON, however its still comes out as a string with my code below

local themeGreen2 = { 100,101,102,103}    

    

function testfunction2()    

    inputStr = “themeGreen2”                        --want to make this point to equivalent table eg themeGreen2 defined above    

–    local str = ‘{ “theme” : ‘…"’"…inputStr…"’}" 

    local str = ‘{“theme”:“themeGreen2” }’            --debug version to test string concat above

    local tab = json.decode(str)

    if type(tab.theme) == “string” then

        print (“XX its a string”, tab.theme)

    elseif     type(tab.theme) == “table” then

        print (“XX its a table”)

        print (“theme table data”, tab.theme[1])

        

    end        

end    

Firstly I’m having issues concating a varible into a string with all the quotes required for JSON see commented out line, the second variable I’ve had to use single quotes.

However with the test string ‘{“theme”:“themeGreen2” }’ the result comes out as a string…

Rob can you shed anymore light on this for me? much appreciated…

Hi there,

For your concatenation code, try the following instead.  (I’m using Lua’s double-bracket notation for strings here, which I think makes it easier in this case.)

[lua]

local str = [[{“theme”:"]] … inputStr … [["}]]

[/lua]

For your second question, tab.theme is indeed a string, namely the string “themeGreen2”, since that’s what it is in the JSON table.  If you want to take that string and “interpret” it as the name of a variable, you could put your themes into a table and then use tab.theme as a key.  More specifically, you coudl change your first line to [lua]local themes = {themeGreen2 = {100,101,102,103}}[/lua], and then you could get the theme by doing [lua]themes[tab.theme][/lua].

  • Andrew

Hi Andrew thanks for your comments:

The Double square brackets are certainly easier to play with, however as you say it still returns a string because it is a string!

Not really thought about your idea for the rest, but it is a good one :slight_smile:

Right now I’m using a table of strings and a table of tables without using JSON, I search for a matching string and then use the same index to find the right / matching theme table, if it can’t find it uses the default.

I wanted to shy away from creating a ‘table of a table’ but in reality its little work to add an entry in the table every time I create a new theme.

Just a pity there’s no easy way for a string to be cast into variable of the same name. However the type() test is very useful

I’m not sure if this is quite what you’re looking for, but I made a library that’ll convert strings to tables and vice-versa (like json.decode and json.encode).

http://www.gymbyl.com/programming/s2t.html

It won’t work with nested tables, though.

C