Accessing a Table as a String

I am using LuaSocket and http.request to call a remote PHP script that generates a Lua table and outputs it to the browser.

When I store the http.request response in a variable it’s a string, which renders the table unusable in my Lua code.

For example:
[lua]eventData = http.request(“http://www.example.com/events.php”)
print( eventData )

–print outputs this string from PHP, that is really a Lua table that PHP generated
months={
‘January’,
‘February’,
‘March’,
‘April’,
‘May’,
‘June’,
‘July’,
‘August’,
‘September’,
‘October’,
‘November’,
‘December’,
}[/lua]

If I try calling months[4], for example, it errors out with “attempt to index global ‘months’ (a nil value)”.
How can I cast that string as a usable table?

Thanks! [import]uid: 4621 topic_id: 1723 reply_id: 301723[/import]

I got some help on StackOverflow:
“You can use loadstring to create a lua chunk that you can execute.”

For example, in my case:
[lua]eventData = http.request(“http://www.example.com/events.php”)
loadstring(eventData)()
print( months[4] )
– months is the table in the original string that is now
– treated as a table, so months[4] now outputs “April” as-expected[/lua] [import]uid: 4621 topic_id: 1723 reply_id: 5056[/import]

The Ansca team kindly informed me today that loadstring is not allowed per Apple TOS. So, while it may work in the simulator, it won’t work on your device builds.

The way I was approaching the solution was impractical anyway. I shouldn’t need PHP to generate a LUA table, so now I am instead passing a string into Lua and then doing the string processing and table creation there.

To quote Walter Luh of Ansca, who said it best:
“You have to generate a string that can be read in as data (e.g. a serialized table or as xml). And then generate the Lua table from that data on the client.”

I don’t like the available XML parsing options, as having no way to maintain the associative qualities of tables makes the XML tough to work with, so I did a whacky solution centered around exploding m string based on custom separators I defined throughout the string and then building the nested tables from there. [import]uid: 4621 topic_id: 1723 reply_id: 5079[/import]