lua-users home
lua-l archive

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



Am 13.09.2016 um 10:35 schrieb Tomas.Lavicka@tieto.com:

Program:
   Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
   > a={}
   > a[1]=1
   > a[2]=nil
   > a[3]=3
   > a[4]=4
   > print(#a)
   4
should returns value 1 but because a[2] is nil, it should return value 1 according book “Programming in Lua (Third edition)” – there is hole in sequence.

No, this is not a bug.  The length operator #, when used on tables, is only defined for sequences.

Above table is not a sequence.  See http://www.lua.org/manual/5.3/manual.html#3.4.7 for details.

btw, "Programming Lua, Fourth Edition" is out, you might consider an "update"...  Especially since
the new edition contains a nice explanation in chapter 5.3 which the length operator on tables
with nil's is such a nice source of confusion...