WoW:API unpack

From AddOn Studio
Revision as of 06:27, 27 November 2009 by WoWWiki>Hans Kamp (→‎Notes: I think only the first 4 values will be assigned to x1, x2, x3 and x4.)
Jump to navigation Jump to search

Returns the values in a consecutive-integer indexed table

ret1, ret2, ... retN = unpack(table[, start][, end])

Will not return values with hash style indexes. For example:

local t = {1, "two", "3", x = "ecks", y = "why?"}
unpack(t) -- returns: 1, "two", "3"

Arguments

Parameters

table
(table) - A table
start
Integer - Starting index, defaults to 1 if nil
end
Integer - Ending index, appends nil values if the table does not contain enough

Returns

ret1, ret2, ... retN
A list of the values contained in the given table.

Examples

local arg1, arg2, arg3 = unpack({1, 5, "Hearthstone"})

-- This does the same thing:
local arg1, arg2, arg3 = 1, 5, "Hearthstone"
unpack({1,2,3,4,5},2,7)  -- returns 2, 3, 4, 5, nil, nil

Notes

Unpack() appears to be buggy when dealing with multiple nil values and the last value is nil.

 t = {1,nil,"something",nil)
 x1,x2,x3,x4 = unpack(t)  -- returns 1,nil

A simple fix that works could be adding an empty value.

 t = {1,nil,"something",nil,1)
 x1,x2,x3,x4 = unpack(t)  -- returns 1,nil,"something",nil

--Peeka, sentinels Template:LUA