lua-users home
lua-l archive

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


For example luajit2 (latest, 32-bit and 64-bit MSVC) returns 16.

On 3/31/2011 8:32 AM, Jan Behrens wrote:
On Thursday 31 March 2011 17:28:12 Arvid Enbom wrote:
Hello.

Run this for some strange results:
print(#{[1]=1,[2]=1,[4]=1,[8]=1,[16]=1,[32]=1,[64]=1,[128]=1})

According to the Lua reference manual, the length operator (#) is supposed
to return the highest consecutive numerical index. This means the above
code SHOULD return 2, because 3 is nil.

It returns 128.

See http://www.lua.org/manual/5.1/manual.html#2.5.5

"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)."

Returning 128 is within specification (as well as returning 2 would be).


Regards
Jan Behrens