Identifying a table

Hi all,

I have a tricky puzzle on my hands (I like to think of Programming as puzzle solving)

I need to identify a table from a series of variables that could be anything - e.g:

[lua]json = require(“json”)
readTable = {}
variableTable = {}

path = system.pathForFile(“file.JSON”)
openedPath = io.open(path, “r”)
readPath = openedPath:read("*a")
variableTable = decode.json(readPath)

for k,v in pairs(variableTable) do
print (k,v)
readTable[k] = v
end[/lua]
The problem here is that the JSON file contains sections within sections and, when the JSON is decoded they appear as tables (Which is fine) I just need an automated way of deciding what a table is so that i can pass that onto a for loop so it is able to load everything into an array.

So, my question is, how do I tell the if statement (Which will decide what is and what isn’t a table) what to evaluate upon - e.g:

[lua]if readTable[k] == whatever A Table Identifier Is then

end[/lua]

(Before you ask, I am unable to upload the JSON file because it contains sensitive information. Sorry!) [import]uid: 65237 topic_id: 30800 reply_id: 330800[/import]

Just an update - I’ve found a work around by turning the array into a string and running a string.find() upon it to check for the word “table”.

This seems very clunky, anybody else got any other ideas?

Thanks in advance! [import]uid: 65237 topic_id: 30800 reply_id: 123272[/import]

Maybe what you need is Lua’s native function type().
For instance, you can loop through nested tables this way:

[code]
local function loop(t)
for k,v in pairs(t) do
if type(v)==‘table’ then loop(v)
else print((“Key: %s - Value: %s”):format(tostring(k),type(v)))
end
end
end

– Tests
loop({x = 1, z = 2,{k = 4, {8, ‘a’, hello}, function(…) end}})
[/code] [import]uid: 142361 topic_id: 30800 reply_id: 123386[/import]

Ohh epic! Thanks so much! You really saved my bacon there! [import]uid: 65237 topic_id: 30800 reply_id: 123402[/import]

Just an update - I’ve found a work around by turning the array into a string and running a string.find() upon it to check for the word “table”.

This seems very clunky, anybody else got any other ideas?

Thanks in advance! [import]uid: 65237 topic_id: 30800 reply_id: 123272[/import]

Maybe what you need is Lua’s native function type().
For instance, you can loop through nested tables this way:

[code]
local function loop(t)
for k,v in pairs(t) do
if type(v)==‘table’ then loop(v)
else print((“Key: %s - Value: %s”):format(tostring(k),type(v)))
end
end
end

– Tests
loop({x = 1, z = 2,{k = 4, {8, ‘a’, hello}, function(…) end}})
[/code] [import]uid: 142361 topic_id: 30800 reply_id: 123386[/import]

Ohh epic! Thanks so much! You really saved my bacon there! [import]uid: 65237 topic_id: 30800 reply_id: 123402[/import]