lua-users home
lua-l archive

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


> 3. As Josh Billings put it, "The trouble with people is not what
> they don't know, but what they know and it ain't so." If your
> sequence ain't a sequence, #t will give you a frontier, i.e.
> a filled position where the next one is empty. So you can
> just keep on doing "t[#t+1]=newvalue" and never overwrite
> anything already in the array. This behaviour used to be
> documented, was removed because it might confuse some
> people, but now that it has become clear that people get
> confused anyway, we hope that the next Lua release might
> restore it, since the implementation has not changed.

We are planning to restore it. The new explanation would go like this.
(What sounds better, "border" or "frontier"?)

--------------------------------------------------------------------------
A *border* in a table 't' is any non-negative integer such that

   (border == 0 or t[border] ~= nil) and t[border + 1] == nil

That is, a border points to any position in a table where a non-nil value
is followed by a nil value (or to 0, when position 1 is empty).

For instance, the table {1, 2, 3, 4, 5} has only one border, 5.  The
table {1, 2, 3, nil, 5} has two borders, 3 and 5.  The table {nil, 2, 3,
nil, 5, nil} has three borders, 0, 3 and 5. The table {} has one border,
0.

Note that any table has at least one border.
Note also that keys that are not a non-negative integer
do not interfere with the notion of borders.
The table {x=1, y=10} has one border, 1. The table
{[1.1] = 1, [-3] = 1, [0] = 1, 1, 2, 3} also has one border, 3.


A table with exactly one border is called a *sequence*.

The length operator #t returns a border for the table 't'.
When t is a sequence, #t returns its only border, which corresponds to
the intuitive notion of the length of the sequence.
When t is not a sequence, #t can return any of its borders. (The exact
one depends on details of the internal representation of the table.)
--------------------------------------------------------------------------

-- Roberto