Published Sep 17, 2023
Contribute to Docs

The unpack() function takes a table and returns the elements of that table as separate values.


result1, result2, ..., resultn = table.unpack(someTable)
  • someTable: The table to be unpacked.
  • result1, result2, ..., resultn: Variables that will receive the individual elements from the table.

Example 1

The following example uses the unpack() function to take the table fruit and unpack its elements into the variables fruit_option1, fruit_option2, fruit_option3. Each variable is assigned one of the elements from the table:

fruit = {'orange','apple','watermelon'}
fruit_option1,fruit_option2,fruit_option3 = table.unpack(fruit)

The example will result in the following output:

Type1: orange
Type2: apple

Example 2

This example works with function addShape to unpack the table values in order to show the X axis and Y axis.

function addShape(x1, y1, x2, y2, x3, y3)
return string.format("X axis: (%f,%f,%f). Y axis: (%f,%f,%f)",x1,x2,x3,y1,y2,y3)
values = {0.7, 1.5, 0.4, 2.8, 1.2, 3.6}

This will result in the following output:

X axis: (0.700000,0.400000,1.200000). Y axis: (1.500000,2.800000,3.600000)

All contributors

Looking to contribute?

Learn Lua on Codecademy