Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How To Convert A String To Table, Or How To Test For A String?
Started by dominic.wood8 Apr 12 2013 08:49 AM

- - - - -
6 replies to this topic
string table json
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dominic.wood8

[GLOBAL: userInfoPane.html]
dominic.wood8
  • Enthusiast

  • 45 posts
  • Corona SDK

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...



[TOPIC: post.html]
#2

aukStudios

[GLOBAL: userInfoPane.html]
aukStudios
  • Corona Geek

  • 1,003 posts
  • Corona SDK

Hi there,

 

Yup, lua has a built in function called type to tell you the type of a variable at runtime.  The code

if type(themeName) == "string" then
will tell you whether themeName is a string.

 

- Andrew



[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,400 posts
  • Enterprise

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.



[TOPIC: post.html]
#4

dominic.wood8

[GLOBAL: userInfoPane.html]
dominic.wood8
  • Enthusiast

  • 45 posts
  • Corona SDK

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...


[TOPIC: post.html]
#5

aukStudios

[GLOBAL: userInfoPane.html]
aukStudios
  • Corona Geek

  • 1,003 posts
  • Corona SDK

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.)

local str = [[{"theme":"]] .. inputStr .. [["}]]

 

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

local themes = {themeGreen2 = {100,101,102,103}}
, and then you could get the theme by doing
themes[tab.theme]
.

 

- Andrew



[TOPIC: post.html]
#6

dominic.wood8

[GLOBAL: userInfoPane.html]
dominic.wood8
  • Enthusiast

  • 45 posts
  • Corona SDK

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 :)

 

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



[TOPIC: post.html]
#7

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Corona Geek

  • 1,424 posts
  • Corona SDK

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




[topic_controls]
[/topic_controls]