lua-users home
lua-l archive

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


On 10/3/13, Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> wrote:
>> Lua table distinguishes sequential keys (from 1 through N) that can be
>> iterated in order using ipairs from the other keys iterated via pairs
>> that visit all the keys in a non-specified order.
>
> No, Lua makes no such distinction. ipairs does because it provides a view
> of the table as a sequence.
>
>> Another related question: How can I iterate sequential part of a table
>> from 1 through N visiting only even (or odd) elements?
>
> The crucial point in knowing N. Here is a variant of ipairs that skips
> nil entries. But it needs N.

This is precisely my point. If a table has holes there is no quick way
(faster than O(N)) to find out N. Therefore, iterating backwards
(decrementing from N down to 1) or with increments larger than 1
(e.g. over odd elements only) are not straightforward.

--Leo--