lua-users home
lua-l archive

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


Quoth Jon Nalley <lists@bluebot.org>, on 2010-03-03 09:11:55 -0600:
> I am having a hard time understanding why I am seeing different
> behavior in the following cases:

An "array" in the Lua making-things-out-of-tables sense can't be
sparse; if it is, then it's up to the vagaries of the implementation
what happens when you try to get its "length".  If you need to have
array-like things with nils in them, there are a number of ways to
get that, such as tracking the length yourself or replacing the nils
with something else; which way you go is dependent on your situation.

>From the Lua 5.1 reference manual:
| 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).

   ---> Drake Wilson