What does table.pack/table.unpack do?

I don’t understand. I also can’t comprehend what they would do based on their name. Help please?

9 Likes

Are you sure? Everyone is talking about them here.

I just posted something like that, you should check it out

tuple kind of means each one

Ok, so what’s pack then?

30char

I’m not sure, but it’s probably the opposite of unpack, like grouping things together instead of separating them

So converting a table into a tuple?

table.pack() takes a tuple and gives you back a table with each element of the tuple as a seperate value:

local myTable = table.pack("This", "That")
print(myTable[1]) --> This

Edit because this was marked as the solution:


table.unpack() takes a table and gives you back a tuple with each value of the table as a seperate element:

local myTable = {"This", "That"}
local val1, val2 = table.unpack(myTable)
prinr(val2) --> That
30 Likes

Oh, I meant tuple into a table. That’s exactly what I thought. Thanks.

table.pack(...) creates an array with all elements passed through, along with an additional field n which stores the amount of arguments passed through. (select can be used to get this, too)

Equivalent:

local function pack(...)
    return {n=select("#",...),...}
end

table.unpack(list,i,j) returns elements i through j of array list. Default for i is 1, and default for j is #list

(unpack doesn’t support metamethods)

list[i],list[i+1],...,list[j]
6 Likes