How would I determine if the value of a key is a table or another dictionary inside a dictionary?

I want to loop through each key in a dictionary and then if the key’s value is a dictionary,table to loop through that too.

local testDictionary = {
	FruitName = "Lemon",
	FruitColor = "Yellow",
	Sour = true,
	test = {hello = "one"}
}
 
for key, value in pairs(testDictionary) do
	print(key, value)
end

here I have tried to see what would happen and the result is strange:

test table: 0x6410c3bccefa9afd
FruitName Lemon
FruitColor Yellow
Sour true

It seems that when looping through, it tells you in a string a bunch of characters most likely encoded from the actual table/ dictionary. How would I quickly determine if the value is a table? It seems that this is possible indirectly by simply taking the string result and determining if the name has the word table. Also if you could, id like to see this combined with getting the values of the table inside the dictionary. I think I would know where to go after determining if table but I want to make sure.

Just check type(value). It’ll return “table” if it’s a table.

4 Likes

I think this could possibly help you out: Detecting Type of Table (Empty, Array, Dictionary, MixedTable) - #15 by XAXA.

Using the information from the post I linked above, if the code finds a value to be a dictionary, then you can run a loop to loop through that.

both are informational. I just wanted if its a table but its good to consider the type of the table too. Currently testing out tables and dictionaries inside of a dictionary

After looking at the link you gave, I noticed that it only gives
“nil” (a string, not the value nil), “number”, “string”, “boolean”, “table”, “function”, “thread”, and “userdata”

Is is possible to determine if the value is a dictionary too?

Actuall I think the second link might help

Since there’s no strict definition of what’s considered a dictionary, an array, or a mixed table, there’s no built-in function to do that. You’d have to settle with something like what Advanced linked, even if it’s a bit arbitrary.

3 Likes

It seems that it checks if the table or dictionary has a key. If it does, then its a dictionary, if not, its a table