lua-users home
lua-l archive

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

On 23/02/2008, Enrico Colombini <> wrote:
> I'm aware that, when using 'next', "The order in which the indices are
>  enumerated is not specified".
>  But my question is:
>  - Is that order always the same, given the following conditions?
>  t = {
>    a = { x=1, y=2 },
>    b = { x=3, y=4 }
>  }
>  for k,v in next, t do
>    -- don't change k (of course)
>    -- don't change v
>    -- may change v.x, v.y
>    -- may add new fields to v
>  end
>  Given those assumptions, is the order of traversal of t always the same
>  in different invocations of this loop (either b,a or a,b in this case)
>  or is this not guaranteed?

I think (but I'm not sure) that, in principle, the order is never
guaranteed. And in practice, the order is always the same as long as
the table is not changed (and in certain cases even after the table is
changed, but I don't think this is usefully predictable).

Changes to tables inside tables (e.g. changes to "v", inside "t" in
your example) should have no impact on the containing table ("t").