lua-users home
lua-l archive

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


Gavin Wraith wrote:
I know my phrasing is longer but as the manual stands in the case that
t[1] = nil, t[2] = 0, t[3] = nil
it would imply that #t = 2.


The original meaning is correct, as for the table you describe #t may indeed be 2. Or it may be 0. It may differ between implementations, and is even allowed to differ between executions of the same line of code.

This is the well known "tables with holes" problem, where to keep Lua efficient a solution has been chosen which makes the # operator fairly unusable on tables with nils in them. A common work around is to separately store the length of the table "t" as "t.n", manipulating it as you add or remove items.

There's no neat way to remove the need for this workaround, but in practice it isn't an issue for 95%+ of tables..

- Alex