pack()
Published Sep 28, 2023
Contribute to Docs
The pack()
function takes multiple argument values, then returns a new table.
Syntax
table.pack(value1, value2, ..., valuen)
value1
,value2
,...
,valuen
: Each argument value will become an element in the new table.
Note: The
n
field is added when passing arguments to thepack()
function wheren
is the number of elements in the table. This field can called using the dot notation syntax. For example,cars.n
will return the number of elements in thecars
table. Remember that Lua table elements will begin with index 1, not 0.
Example
The following example uses the pack()
function to take multiple argument values like Watermelon
, Bananas
, Apple
, and Oranges
to create the table fruit
.
fruit = table.pack('Watermelon','Bananas','Apple','Oranges')print("Table: ",fruit)print("Fruit number 2:",fruit[2])print("Total number of fruit: ",fruit.n)
The example will result in an output similar to the following:
Table: table: 0x55b6ebb24400Fruit number 2: BananasTotal number of fruit: 4
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Lua on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Lua
Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.Beginner Friendly4 hours