lua-users home
lua-l archive

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

I had a similar need lately, making "fingerprints" for table contents.

Did it by collecting the field fingerprints into a table, then sorting the table, then concatenating. Sorting guarantees iteration order did not matter.

	"x=1", "y=2"	-> sorted, concat	->	"{ x=1, y=2 }"
	"x=3", "y=4"	-> sorted, concat	->	"{ x=3, y=4 }"
	"{ a={x=1,y=2}, b={x=3,y=4} }"

Such a string was used as a cache key in luaSub constraints management.


Enrico Colombini kirjoitti 23.2.2008 kello 17:04:

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

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?