Pack: Difference between revisions

From Pico-8 Wiki
Jump to navigation Jump to search
imported>Frederic.souchu
mNo edit summary
imported>Felice Enellen
(fix category)
 
(5 intermediate revisions by 2 users not shown)
Line 4: Line 4:
|...||parameters|||
|...||parameters|||
}}
}}
Returns a new table with all parameters stored into keys 1, 2, etc
Returns a new table with all parameters stored sequentially into keys [1], [2], etc.
 
The table will also contain a member <var>.n</var> which specifies how many arguments were passed to <code>pack()</code>, including <var>nil</var> arguments. This can be used to detect the difference between an argument that was not passed at all and a <var>nil</var> value that was deliberately passed, since the <code>#</code> operator stops when it reaches a <var>nil</var> value.
 
== Examples ==
<syntaxhighlight lang="lua">
tbl = pack(x, y, dx, dy)  -- returns {x, y, dx, dy}
print(#tbl)                -- prints "4"
print(tbl.n)              -- prints "4"
 
tbl = pack(x, y)          -- returns {x, y}
print(#tbl)                -- prints "2"
print(tbl.n)              -- prints "2"
 
tbl = pack(x, y, nil, nil) -- returns {x, y}
print(#tbl)                -- prints "2"
print(tbl.n)              -- prints "4"
</syntaxhighlight>
 
== See also ==
* [[Unpack|<code>unpack()</code>]]
[[Category:Reference]]
[[Category:Reference]]
[[Category:Api]]
[[Category:API]]
[[Category:Lua]]
[[Category:Lua]]

Latest revision as of 21:35, 11 March 2021

pack( ... )
Creates a table from the given parameters.
...
parameters

Returns a new table with all parameters stored sequentially into keys [1], [2], etc.

The table will also contain a member .n which specifies how many arguments were passed to pack(), including nil arguments. This can be used to detect the difference between an argument that was not passed at all and a nil value that was deliberately passed, since the # operator stops when it reaches a nil value.

Examples

tbl = pack(x, y, dx, dy)   -- returns {x, y, dx, dy}
print(#tbl)                -- prints "4"
print(tbl.n)               -- prints "4"

tbl = pack(x, y)           -- returns {x, y}
print(#tbl)                -- prints "2"
print(tbl.n)               -- prints "2"

tbl = pack(x, y, nil, nil) -- returns {x, y}
print(#tbl)                -- prints "2"
print(tbl.n)               -- prints "4"

 See also