lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Apr 16, 2012, at 10:31 AM, csrl@gmx.com wrote:

> For a further (related?) look try variations of print(table.unpack(table.pack(a,b[..,z]))) where some of the parameters are nil.  Sometimes the unpack list contains the same nil parameters, others not.

Perhaps worthwhile mentioning that pack/unpack are mostly useful in terms of varargs. 

table.pack holds an additional 'n' field, to keep track of how many effective elements there is:

http://www.lua.org/manual/5.2/manual.html#pdf-table.pack

table.pack is equivalent to the following Lua code:

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

print( pack( nil, nil, nil ).n )

> 3

To properly unpack a, hmmm, packed table, you need to explicitly specify it's length:

local varargs = table.pack( nil, nil, nil )

print( #varargs )
print( varargs.n )
print( table.unpack( varargs, 1, varargs.n ) )

> 0
> 3
> nil	nil	nil