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
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?
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
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