What do you want to achieve? I want to unpack all the tables in a table.
What is the issue? Well I don’t know much tables there are so I don’t know how much times to loop
What solutions have you tried so far? I tried to make my own function with while but it seems too complicating
function getArgs(Table)
-- epic code shit that unpacks all the tables inside the table
print(table.unpack(result))
end
getArgs({"test", {"test2", {"hi"}}})
Is there a method I’m missing or is this impossible?
function getArgs(Table)
local result = {};
print(Table)
for _, Value in pairs(Table) do
local unpacked = type(Value) == "table" and {getArgs(Value)} or Value
if type(unpacked) == "table" then
for _, Value in pairs(unpacked) do
table.insert(result, Value)
end
else
table.insert(result, unpacked)
end
end
return unpack(result)
end
Yeah, Thats what I was looking for… but you reset the ‘resutlt’ in the function so I might consider assigning result out of the function. Also I thought of this but I didn’t think it was this easy
local BaseTable = {
3,
{
2,
"Bob",
Vector3.new(3,4,6),
{
"BobValue"
}
}
}
local function UnpackTables(var : table)
local newtable = {}
for _,v in pairs(var) do
if typeof(v) == "table" then
for _,v1 in pairs(UnpackTables(v)) do
table.insert(newtable,v1)
end
else
table.insert(newtable,v)
end
end
return newtable
end
local FixedTable = UnpackTables(BaseTable)
print(FixedTable)
function getArgs(Table)
local result = {};
print(Table)
for _, Value in pairs(Table) do
local unpacked = type(Value) == "table" and {getArgs(Value)} or Value
if type(unpacked) == "table" then
for _, Value in pairs(unpacked) do
table.insert(result, Value)
end
else
table.insert(result, unpacked)
end
end
return unpack(result)
end
print(getArgs({"test", {"test2", {"hi"}}}))