Pack: Difference between revisions
Jump to navigation
Jump to search
imported>Felice Enellen (different var name so we don't trigger the generic syntax highlighting of the word "table") |
imported>Felice Enellen (fix category) |
||
| Line 26: | Line 26: | ||
* [[Unpack|<code>unpack()</code>]] | * [[Unpack|<code>unpack()</code>]] | ||
[[Category:Reference]] | [[Category:Reference]] | ||
[[Category: | [[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"