[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua, LuaJIT2 and differences with the length operator
- From: Matthew Wild <mwild1@...>
- Date: Wed, 29 Dec 2010 18:53:21 +0000
On 29 December 2010 16:07, Daniele Alessandri <suppakilla@gmail.com> wrote:
> On Wed, Dec 29, 2010 at 16:39, Duncan Cross <duncan.cross@gmail.com> wrote:
>
>> You are doing it wrong by trying to rely on that. As the manual
>> states, "#t can be any of the indices" - *any* of them, it is
>> deliberately undefined which. You should treat it as if it is random,
>> even though it may seem to be consistent.
>>
>> In fact, it is not even consistent in Lua 5.1. For example, these two
>
> OK, this confirms to me that a piece of my code was working on Lua 5.1
> by pure coincidence :-)
> I'll handle it differently instead of just passing the user-provided
> table to unpack (see my other reply to Luiz).
>
A common idiom (blessed by an official table.pack() in 5.2):
function pack(...)
return { n = select("#", ...), ... }; -- Save the number of items in 'n'
end
-- Usage:
mytable = pack(1, nil, 3);
print(unpack(mytable, 1, mytable.n)) -- Use number of items instead of '#'
Regards,
Matthew