List three-dimensional table

Asked

Viewed 105 times

4

I have a table that I intend to print out all your 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 in each variable?

1 answer

5


Some problems:

The example of the code is not a three-dimensional table. If so, the following algorithm needs to be modified, but I think we can already understand how to do when there is nesting tables (in the background even this example can not be treated with something of various dimensions)

The declaration of 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

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.