Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua Nested Unpack Bug?

Tags:

lua

Question:

I'm trying to unpack an array into an array, but it only works if it's the last item unpacked, if there is anything after it only the first element is unpacked. The following is a very basic example of what I'm trying to do. Is there a better way to do this, or is this a bug I'll have to cope with? I don't want to use table.insert as this seems to be much more readable adding within the definition of the table with something like unpack.

Code:

   print ("Error 1")
   local table1 = { {1,1}, {2,2}, {3,3} }
   local table2 = { {0,0}, unpack (table1), {4,4} }
   for n,item in ipairs (table2) do print (unpack(item)) end

   print ("Good")
   table1 = { {1,1}, {2,2}, {3,3} }
   table2 = { {0,0}, unpack (table1) }
   for n,item in ipairs (table2) do print (unpack(item)) end

   print ("Error 2")
   table1 = { {1,1}, {2,2}, {3,3} }
   table2 = { {0,0}, unpack (table1), unpack (table1) }
   for n,item in ipairs (table2) do print (unpack(item)) end

Output:

Error 1
0       0
1       1 -- {2,2} & {3,3} cut off.
4       4
Good
0       0
1       1 -- All elements unpacked.
2       2
3       3
Error 2
0       0
1       1 -- {2,2} & {3,3} cut off.
1       1 -- All elements unpacked.
2       2
3       3

Note:

I'm running version 5.1.

like image 601
Kyle James Walker Avatar asked Sep 02 '26 19:09

Kyle James Walker


1 Answers

This is not a bug. A function call that returns multiple values is adjusted to the first value if the call is not the last one. The manual says that at http://www.lua.org/manual/5.1/manual.html#2.5

like image 180
lhf Avatar answered Sep 05 '26 15:09

lhf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!