Log In  

If unpack is used to populate a new table, and the unpack is followed by even more items, then only the first item from the unpack gets retained.

function print_tbl(t)
 ?'count:'..#t
 for k,v in pairs(t) do
  ?k..'='..v
 end
end

t0={
 a='a',b='b',
 unpack(split"1,2,3,4")
}
print_tbl(t0) -- this works as expected

t1={
 unpack(split"1,2,3,4"),
 a='a',b='b'
}
print_tbl(t1) -- 2,3,4 stomped out

t2={
 unpack(split"1,2,3,4"),
 unpack(split"5,6")
}
print_tbl(t2) -- 2,3,4 stomped out
P#88947 2021-03-24 09:42

1

Unfortunately, that's just how Lua works. The implicit tuple of a '...' argument, or the return values from a function, are only appended in full if it's the last one in a new tuple, e.g. an arglist, an assignment list, or a table list. Otherwise you only get the first value.

P#89595 2021-03-26 07:10 ( Edited 2021-03-28 03:25)

Bummer. Thanks for clarifying!

P#89641 2021-03-27 00:10

[Please log in to post a comment]