This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
EveryOS's profile picture

Distribute table as individual arguments

Started by EveryOS, 21 March 2016 - 12:13 AM
EveryOS #1
Posted 21 March 2016 - 01:13 AM
I have a table and a function. There is no guaranteed amount of values in this table, but I need to use all of them, and as individual arguments for this function. I only want to run this function once. Can I distribute the items in my table as individual arguments for the function?
Lyqyd #2
Posted 21 March 2016 - 02:22 AM
You'd use unpack or table.unpack, depending on your version.

You should check out the lua-users wiki or the Programming In Lua document, or even consult the reference manual.
Exerro #3
Posted 22 March 2016 - 08:35 PM
It works like this:


local x, y, z, w = unpack( {1, 2, 3, 4} )
-- x = 1, y = 2, z = 3, w = 4

You could even write a rough equivalent in Lua…


local function lua_unpack(t, n, f) -- table, starting position, ending position
     n = n or 1
     if f and n > f then
          return nil
     end
     return t[n], t[n + 1] and unpack( t, n + 1, f )
end