Is it possible to iterate through multiple tables using 1 for loop?

Question is in the topic. Just curious if this is possible. I’ve tried looking on the devforum, but I never got a clear concise answer.

The main goal of this is to iterate through multiple subtables or any table

e.g.

--using subtable dictionaries for e.g.
local TestTable = {

Table1 = {
Red = true
Blue = true
Green = true
};

Table2 = {
Red = true
Blue = true
Green = true
};

}

I want to print the Key and Value of both subtables using only 1 for loop.
So print something like


Red: true
Blue: true
Green: true
Red: true
Blue: true
Green: true

I can do

for key, value in pairs(TestTable.Table1) do

end

-- and

for key, value in pairs(TestTable.Table2) do

end


But to me it seems like I’m repeating myself and inefficient overall…

I have also tried something like this but…

for key, value in pairs(TestTable) do -- this gets the main table hope that's the correct term
    for k,v in pairs(key) do -- I get the key which are the subtables
     -- but it recognizes the key as a string and not a table and I get this error
   end
end

invalid argument #1 to 'pairs' (table expected, got string); --most likely cause the args for a for loop are strings lol so there's that
--also that's 2 loops

So is it possible to do this? If not is there a different way to accomplish this without using multiple for loops as I stated above?

Are you sure value isn’t the subtable variable, because key is just the string(or nurmber) that value is assigned to.

The value is the dictionary I want to print.

['Red'] = true
['Blue'] = true
['Green'] = true

So it doesn’t actually give me the key and value separately, it returns the whole dictionary, which I do not want.

So if I wanted to assign something with certain properties, it’d give me an error. If that makes sense

local t = {
    t1 = {
        red = true,
        blue = true,
        green = false
    },
    t2 = {
        red = false,
        blue = false,
        green = true
    }
}

for i,v in pairs(t) do
    if type(v) == "table" then
        table.foreach(v, print)
    end
end
1 Like
local TestTable = {

	Table1 = {
		Red = true,
		Blue = true,
		Green = true
	};

	Table2 = {
		Red = true,
		Blue = true,
		Green = true
	};

}

for i, Table in pairs(TestTable) do
	for i, v in pairs(Table) do
		print(string.format("%s: %s", i, tostring(v)))
	end
end

-- OnE fOr LoOp
for i, Table in pairs(TestTable) do
	table.foreach(Table, function(i, v)
		print(string.format("%s: %s", i, tostring(v)))
	end)
end
2 Likes

You could look into using some recursion. Not sure why people are recommending table.foreach – it is deprecated.

local function deep_search(tbl)
    for key, value in pairs(tbl) do
        if typeof(value) == "table" then
            deep_search(value)
        end
        print(key, value)
    end
end
11 Likes

Thank you all for the replies. I just have one question cause I actually feel dumb. Why is the value the table and not the key? Cause if I do

for key,value in pairs(TestTable) do
   print(key)
end

I get TestTable1 and TestTable2. So wouldn’t that be the table?

What exactly do you mean by key?

It’s because you’re basically dealing with a dictionary where the syntax is:
local dict = {<key> = <value>}.
When you’re using

local TestTable = {
    Table1 = {
    Red = true
    Blue = true
    Green = true
    };

You make a dictionary named “TestTable” where the key is “Table1” and the value assigned to it is the actual table. By doing

for key,value in pairs(TestTable) do
   print(key)
end

You print the keys, aka the “Table1”, “Table2” string.
For more info check out this:https://education.roblox.com/en-us/resources/intro-to-dictionaries---series

1 Like

What I mean is the Key is the subtable I want so it prints the subtable names

--outputs
TestTable1
TestTable2

But @WhosThatGuy_Guys has answered that question for me. Thank you very much. And thank you for your replies everyone.