Unpack table in table in table in table [SOLVED]

  1. What do you want to achieve? I want to unpack all the tables in a table.

  2. What is the issue? Well I don’t know much tables there are so I don’t know how much times to loop

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

when you unpack the table, where do you want its values to be stored?
Inside the primary table or be returned from the function?

In the ‘result’ table. Also inside the primary table since im not returning

do something like this

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

something like this?

1 Like

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 :stuck_out_tongue:

I was making a remote spy with __namecall





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)
1 Like

this wont work since when unpacking you can have multiple values

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"}}}))

I realized that just now too lol

2 Likes

Will look into that. If it works.

Works perfectly! Just as I needed. Thank you all for your help.

1 Like