lua-users home
lua-l archive

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

Enrico Colombini wrote:
> 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?

The traversal order stays the same as long as t is not modified
inbetween. But this is specific to the current implementation of
the Lua VM. The GC doesn't modify tables (except weak tables).

If you want to rely on that (since you embed a particular Lua
version, anyway) -- fine. But don't complain, if it breaks with a
future version or a different VM.

Note: I said it "stays" the same between traversals, not that the
traversal order is always the same for the same keys. Example:

$ lua -e 'table.foreach({a=1,b=1,f=1},print)'
a       1
f       1
b       1

$ lua -e 'table.foreach({f=1,b=1,a=1},print)'
a       1
b       1
f       1