lua-users home
lua-l archive

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


Is there a set of limitations (like – “keys can only be strings”) that if I follow them, pairs() will produce identical iteration order?

As other people have been saying, even tables with only string keys and identical data aren't guaranteed to iterate in the same order.  The hash table typically has collisions, and in the case of a collision, the first value put in the slot will be the first value inserted in the table.  So the history of how the table was created, along with its contents, affects iteration order.  Deletions similarly create trouble.

I have 2 machines, running the exact same code (both Lua and C). They both start from a clean state. The order of insertions is identical and the data being inserted is also identical.

In this case, I suspect that iterations over everything other than userdata and function keys will be deterministic.  I don't trust my own knowledge of Lua's internals enough to say that with total confidence -- but, a quick reading of ltable.c suggests it's true.

-Sven