# Identifying a table

**URL:** <https://forums.solar2d.com/t/identifying-a-table/306559>\
**Category:** Lua Language\
**Created:** [September 10, 2012, 8:47am UTC](https://forums.solar2d.com/t/identifying-a-table/306559 "2012-09-10T08:47:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatthewCharlesHarrop](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@MatthewCharlesHarrop](https://forums.solar2d.com/u/MatthewCharlesHarrop)\
**Post date:** [September 10, 2012, 8:47am UTC](https://forums.solar2d.com/t/identifying-a-table/306559/1 "2012-09-10T08:47:45Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![MatthewCharlesHarrop](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@MatthewCharlesHarrop](https://forums.solar2d.com/u/MatthewCharlesHarrop)\
**Post date:** [September 10, 2012, 9:00am UTC](https://forums.solar2d.com/t/identifying-a-table/306559/2 "2012-09-10T09:00:54Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![roland.yonaba](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/a587f6/32.png) [@roland.yonaba](https://forums.solar2d.com/u/roland.yonaba)\
**Post date:** [September 11, 2012, 5:05am UTC](https://forums.solar2d.com/t/identifying-a-table/306559/3 "2012-09-11T05:05:45Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![MatthewCharlesHarrop](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@MatthewCharlesHarrop](https://forums.solar2d.com/u/MatthewCharlesHarrop)\
**Post date:** [September 11, 2012, 8:27am UTC](https://forums.solar2d.com/t/identifying-a-table/306559/4 "2012-09-11T08:27:01Z")

</div>

Ohh epic! Thanks so much! You really saved my bacon there! [import]uid: 65237 topic\_id: 30800 reply\_id: 123402[/import]

---

<div class="post-metadata">

**Author:** ![MatthewCharlesHarrop](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@MatthewCharlesHarrop](https://forums.solar2d.com/u/MatthewCharlesHarrop)\
**Post date:** [September 10, 2012, 9:00am UTC](https://forums.solar2d.com/t/identifying-a-table/306559/5 "2012-09-10T09:00:54Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![roland.yonaba](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/r/a587f6/32.png) [@roland.yonaba](https://forums.solar2d.com/u/roland.yonaba)\
**Post date:** [September 11, 2012, 5:05am UTC](https://forums.solar2d.com/t/identifying-a-table/306559/6 "2012-09-11T05:05:45Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![MatthewCharlesHarrop](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/e495f1/32.png) [@MatthewCharlesHarrop](https://forums.solar2d.com/u/MatthewCharlesHarrop)\
**Post date:** [September 11, 2012, 8:27am UTC](https://forums.solar2d.com/t/identifying-a-table/306559/7 "2012-09-11T08:27:01Z")

</div>

Ohh epic! Thanks so much! You really saved my bacon there! [import]uid: 65237 topic\_id: 30800 reply\_id: 123402[/import]
