how do I make a table go showing tables

1

I wanted to do a function that does type alg as the thing below

for i in pairs(v) do
    if type(v[i])=="table" then
        for j in pairs(v[i]) do
             if type(v[i][j])=="table" then
                 print("...")
             else
                 print(i,j,v[i][j])
             end
        end
    else
        print(i,v[i])
    end
end

but that works for undefined numbers of tables within others, type 2, 3, 4, 5}, {6}, 7}, {8}, 9}

Is it possible to do a function for this?

link --example quoted above xP

    
asked by anonymous 13.11.2016 / 20:08

2 answers

2

You need a recursive function. See the example below on the Moon online demo . Connect dump to your table instead of _G .

-- globals.lua
-- show all global variables

local seen={}

function dump(t,i)
    seen[t]=true
    local s={}
    local n=0
    for k in pairs(t) do
        n=n+1 s[n]=k
    end
    table.sort(s)
    for k,v in ipairs(s) do
        print(i,v)
        v=t[v]
        if type(v)=="table" and not seen[v] then
            dump(v,i.."\t")
        end
    end
end

dump(_G,"")
    
17.07.2017 / 23:08
1

Or even easier, use the

json.cod(tabela)

--saída: { "matriz": [ "filhos" : [ "netos" : [ 1,2,3] ] ]}
    
11.08.2017 / 04:16