WoW:API unpack: Difference between revisions
Jump to navigation
Jump to search
(→Notes: I think only the first 4 values will be assigned to x1, x2, x3 and x4.) |
m (Move page script moved page API unpack to API unpack without leaving a redirect) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{ | {{wowlua}} | ||
Returns the values in a consecutive-integer indexed table | Returns the values in a consecutive-integer indexed table | ||
ret1, ret2, ... retN = unpack(table[, start | ret1, ret2, ... retN = unpack(table[, start [, end]]) | ||
Will not return values with hash style indexes. | Will not return values with hash style indexes. For example: | ||
local t = {1, "two", "3", x = "ecks", y = "why?"} | local t = {1, "two", "3", x = "ecks", y = "why?"} | ||
unpack(t) -- returns: 1, "two", "3" | unpack(t) -- returns: 1, "two", "3" | ||
== Arguments == | == Arguments == | ||
;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 === | === Returns === | ||
;ret1, ret2, ... retN | |||
: A list of the values contained in the given table. | |||
== Examples == | == Examples == | ||
Line 25: | Line 28: | ||
== Notes == | == Notes == | ||
unpack() doesn't return all the values in the following example because it is not a contiguous table. | |||
t = {1,nil,"something",nil | -- Notes Example 1 | ||
t = {1,nil,"something",nil} | |||
print(unpack(t)) -- prints 1 | |||
A simple fix | A simple fix would be to add a value at the end of the list. This works because when you assign a table in this manner, Lua treats it as a contiguous table as long as there is a value at the last index. Nuking this value before adding new values at the end will break this behavior. | ||
t = {1,nil,"something",nil,1 | -- Notes Example 2 | ||
t = {1,nil,"something",nil,1} | |||
print(unpack(t)) -- prints 1 nil "something" nil 1 | |||
t[5] = nil | |||
print(unpack(t)) -- prints 1 | |||
-- | If you assign each value individually, unpack() will behave the same way it does in Notes Example 1. | ||
{ | -- Notes Example 3 | ||
t = {} | |||
t[1] = 1 | |||
t[2] = nil | |||
t[3] = "something" | |||
t[4] = nil | |||
t[5] = 1 | |||
print(unpack(t)) -- prints 1 even though it's the same contents as in Notes Example 2. |
Latest revision as of 04:47, 15 August 2023
← WoW Lua
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[edit]
- 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[edit]
- ret1, ret2, ... retN
- A list of the values contained in the given table.
Examples[edit]
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[edit]
unpack() doesn't return all the values in the following example because it is not a contiguous table.
-- Notes Example 1 t = {1,nil,"something",nil} print(unpack(t)) -- prints 1
A simple fix would be to add a value at the end of the list. This works because when you assign a table in this manner, Lua treats it as a contiguous table as long as there is a value at the last index. Nuking this value before adding new values at the end will break this behavior.
-- Notes Example 2 t = {1,nil,"something",nil,1} print(unpack(t)) -- prints 1 nil "something" nil 1 t[5] = nil print(unpack(t)) -- prints 1
If you assign each value individually, unpack() will behave the same way it does in Notes Example 1.
-- Notes Example 3 t = {} t[1] = 1 t[2] = nil t[3] = "something" t[4] = nil t[5] = 1 print(unpack(t)) -- prints 1 even though it's the same contents as in Notes Example 2.