Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to iterate all content in a table using loops?

Asked by 8 years ago

Please note:

I know how to iterate a table, this question is basically asking how to iterate ALL content in a table. For example, if you have a table like: local t = {a = 1, b = {1,2,3}} , I'd want to return a table with all it's unpacked data, assuming there are nested tables in it. Like getting all descendants of an object, except instead of iterating an object, it'd be a table.

Question

I know how I could iterate all content in a table with recursive functions, but is there a way to do it with loops instead? This is my current code for my current table iteration:

local function tree(t)
    local all = {}
    local function add(x)
        for i,v in next, x do
            all[#all+1] = v
            if type(v) == "table" then
                add(v)
            end
        end
    end
    add(t)
    return all
end

for i,v in next, tree{
    a = 1,
    b = {1,2,3}
} do print(i,v) end -- prints all values

But I'd like a looped alternative a lot more. If anyone knows how or if this is possible, I'd appreciate the explanation. Thanks.

2 answers

Log in to vote
0
Answered by 8 years ago

To iterate through nested tables, you can used nested loops inside of nested functions.

local test = {1,2,3,a = {4,5,b = {6}}}

local function printall(tab)
    for i,v in pairs(tab) do
    if type(v) == "table" then --if v is a table then
    printall(v) --run the function again on the value v
    else
    print(v)
    end
    end
end

printall(test)

additionally, you could use coroutines so that iterating through the nested tables wouldn't effect the rest of the code.

local test = {1,2,3,a = {4,5,b = {6}}}

local function printall(tab)
    for i,v in pairs(tab) do
    if type(v) == "table" then --if v is a table then
    coroutine.resume(coroutine.create(printall),v) --run the function again on the value v under a new thread
    else
    print(v)
    end
    end
end

coroutine.resume(coroutine.create(printall),test)
Ad
Log in to vote
-3
Answered by 8 years ago

EDITED

You could use a while loop:

local tab = {}
local num = 0

while num < #tab do
    num = num + 1
    if type(tab[num]) == "table" then
        for _,v in pairs (tab[num]) do
            print(v)
        end
    else
        print(tab[num])
    end
end

or a repeat loop:

local tab = {}
local num = 0

repeat
num = num + 1
if type(tab[num]) == "table" then
    for _,v in pairs (tab[num]) do
        print(v)
    end
else
    print(tab[num])
end
until
num == #tab

Both basically continue until num is equal to #tab.

type() is a function used to get the type of value you are putting into it's parameters.

0
I know how to iterate a table. My question is asking how to iterate all values of a table, assuming there are nested tables inside of the one I'm iterating. CodingEvolution 490 — 8y
0
Edited. TheDeadlyPanther 2460 — 8y
0
your solution doesn't iterate past the first nested table. It should work with infinitely nested tables. aquathorn321 858 — 8y

Answer this question