lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi everyone,

I just noticed a subtle difference between using the length operator
on tables containing nil values on Lua and LuaJIT2 and I would like to
get a clarification:

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> t = { 'foo', nil, 'bar' }
> print(#t)
3

LuaJIT 2.0.0-beta5 -- Copyright (C) 2005-2010 Mike Pall. http://luajit.org/
JIT: OFF CMOV SSE2 SSE4.1 fold cse dce fwd dse narrow loop abc fuse
> t = { 'foo', nil, 'bar' }
> print(#t)
1

Which one is right? I guess Lua, even if
http://www.lua.org/manual/5.1/manual.html#2.5.5 states that "If the
array has holes (that is, nil values between other non-nil values),
then #t can be any of the indices that directly precedes a nil value
(that is, it may consider any such nil value as the end of the
array).".

I would expect at least the same behavior, or am I just doing it wrong
by trying to rely on the current one showed by Lua 5.1?

Thanks,
Daniele

-- 
Daniele Alessandri
http://clorophilla.net/
http://twitter.com/JoL1hAHN