lua-users home
lua-l archive

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


13.09.2016 11:53, Marc Balmer пишет:

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



I think this explanation must be in official doc if it's a source of confusion.

print(# {[1] = 10, [2] = 20, [3] = nil, [4] = nil, [5] = 40} ) -- is not sequence ?
print(# {10, 20, nil, nil, 40} ) -- is sequence ?  From doc: Note that a table like {10, 20, nil, 40}  is not a sequence...

output:

2
5