WoW:API unpack: Difference between revisions

576 bytes added ,  27 November 2009
m
→‎Notes: I R has gud grammar...
(→‎Notes: I think only the first 4 values will be assigned to x1, x2, x3 and x4.)
m (→‎Notes: I R has gud grammar...)
Line 25: Line 25:


== Notes ==
== Notes ==
Unpack() appears to be buggy when dealing with multiple nil values and the last value is nil.
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
   x1,x2,x3,x4 = unpack(t)  -- returns 1,nil
   t = {1,nil,"something",nil}
   print(unpack(t))  -- prints 1


A simple fix that works could be adding an empty value.
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
   x1,x2,x3,x4 = unpack(t)  -- returns 1,nil,"something",nil
   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.


--Peeka, sentinels
{{LUA}}
{{LUA}}
Anonymous user