List three-dimensional table

4

I have a table that I want to print all its values.

Example:

local table_2 = {
   ["tabela1"] = "360Mhz", "demo", "teste",
   ["tabela2"] = "360Mhz", "demo", "teste"
}
for k,v in pairs(table_2) do
   print(k,v)
end

How can I print the 4 values for each line and each variable?

    
asked by anonymous 20.08.2015 / 12:16

1 answer

5

Some issues:

The code example is not a three-dimensional table. If this is the case, then the following algorithm needs to be modified, but I think you can figure out how to do it when there is table nesting. (Bottom line, even this example can not be handled with anything of several dimensions.)

The declaration of the internal tables are missing delimiters.

local table_2 = {
   ["tabela1"] = { "360Mhz", "demo", "teste" },
   ["tabela2"] = { "360Mhz", "demo", "teste" }
}
for k, v in pairs(table_2) do
   print(k)
   for k2, v2 in pairs(v) do
      print("   ", v2)
   end
end

See working on ideone .

Obviously you can adapt to print the way you want but the important thing is to scan the external table and get its members that are tables to scan the members again. If there is a need for one more level of nesting, just do the same and include another% internal%.

    
20.08.2015 / 12:39