unpack

Unpacks a table into individual items

Prototype

t1, t2, ... = unpack (t)

Description

Returns all elements from the given list (table) as individual values. This is equivalent to:

return t [1], t [2], t [3] ..., t [n]

t = { "the", "quick", "brown" }

print (unpack (t)) --> the quick brown
An example of using unpack is the case of a variable number of arguments to a function, where you want to pass those to another function.

For example, to make an error function that takes a formatted string:

function ferror (fmt, ...)
  error (string.format (fmt, unpack (arg)), 2)
end -- ferror
However in Lua 5.1 this is more simply written as:

function ferror (fmt, ...)
  error (string.format (fmt, ...), 2)
end -- ferror

Lua functions

Topics