lua-users home
lua-l archive

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

It allows for an efficient implementation of the # operator. What
happens (I think) is that as positive indices are added to the table,
Lua searches for any index where t[index] exists and t[index + 1] is
nil. This saves traversing the whole table when adding one element,
which would be a performance hit.

As everyone says, keep your table hole-free and there will be no
problems. An alternative is table.maxn(): see


2009/12/29 Klaus Ripke <>:
> On Tue, Dec 29, 2009 at 06:22:23PM +0800, h visli wrote:
>> Yes, I also noticed those words, but it still confusing me, maybe my English is poor, can you put more detailed explanation? Thanks In Advance!
> hmm, how to explain "can be any"?
> Does this make any sense to you?
> Anyway, another attempt using different words:
> The implementation is allowed to return an arbitrary
> "index n such that t[n] is not nil and t[n+1] is nil".
> In your example both 1 and 3 are allowed.
> The definition does not say it must be the lowest or largest such n
> to allow an efficient implementation.
> Still it's well defined for arrays without holes.
> best