lua-users home
lua-l archive

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




On Sunday, June 9, 2013, Dirk Laurie wrote:
2013/6/9 Tim Hill <drtimhill@gmail.com>:
> As the Lua docs state, an array is a table with a sequence
> of elements from 1..N.
>
> But my question is: is it ok to build the array out of order?
> For example:
>
> a = { 1, [3]=3 }
> a[2]=2
>
> Is "a" an array?

The actual term is "sequence" but I know what you mean. Yes, it is.

> Lua *seems* to think it is (#a returns 3), but the Lua docs seems
> to be a bit vague on this (it seems to be implied by the term
> "sequence" in the ref manual).

Not vague, but not labouring the point either. Let's say the Manual
is mathematically precise on the subject.

    We use the term sequence to denote a table where the set of all
    positive numeric keys is equal to {1..n} for some integer n,
    which is called the length of the sequence.

Actually, the routines that depend on this property are not confused
by positive numeric keys that can't be taken for integers, but the
above is what it says.

I've been confused on a related matter:

I know that in the current implementation, a sequence  that also contains hashes will still work as a sequence. That is, in my experience:

T = {"at one", "at two", property = "some value"}

...works in ipairs and with the length operator. Is this behavior that is dependent on the implementation or is it something that is a formal part of Lua?

-Andrew