lua-users home
lua-l archive

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


... Continuing with the above, the issue is actually in "getn":

"...Otherwise, the "size" is one less the first integer index with a nil
value."

This behaviour is reasonable, I think, even though it obviously does not
yield the correct value in sparse arrays.

So it is mandatory to set the size explicitly in sparse arrays.
Fortunately, the table library automatically maintains the size once
created; so the only times you need to worry about it are:

1) When creating the sparse array in the first place.
2) When manually inserting elements into the sparse array which might be
beyond the end of the array.

The code I presented in my first (erroneous) message does accomplish that.
What it does not accomplish is the case where you manually delete the last
element in the array. I don't think it should: table.remove() does the
right thing in that case, and for a sparse array there needs to be some way
to distinguish between deleting the last element, and just setting it to
nil. (Depending on the precise application of the sparse array, of course.)

R.