lua-users home
lua-l archive

Re: Predicting ipairs (Was: Lua 5.2 Length Operator)

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


>> Am I correct that # is the size of the array part of a table?
> No.
Indeed. Specifically:
"The length of a table t is defined to be any integer index n such
that t[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n
can be zero. For a regular array, with non-nil values from 1 to a
given n, its length is exactly that n, the index of its last value. 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)."
In your example, 4 is valid because slot 4 is non-nil and slot 5 is
nil. But it could also have been 2, because slot 2 is non-nil and slot
3 is nil. Practically speaking you can only rely on the value of # for
dense arrays.
/s/ Adam

AltStyle によって変換されたページ (->オリジナル) /