lua-users home
lua-l archive

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


I've run into trouble with 'n' too, where I would like to foreach
over a table but then I have to special case out the 'n' element.
There was some discussion about changing 'n' to getn()/setn() which
IMO is a great idea, but I don't know if it went anywhere.

Jason



paul@theV.net wrote:
The following code:

	a = { "a", "b", "c" }
	for i, v in a do write(v) end
	tremove(a)
	for i, v in a do write(v) end
foreach
produces "abcab2" as the result.

Then I checked the Lua manual, "tremove" removes the last element and
then inserts extra field "n = getn(table)". But this makes
the array table into a mapping table. I mean, is this a desirable
behavior? for any good reasons?

Tentatively, I change to foreachi in my programs, but writing extra
anonymous function isn't as clean, as I am forced to use upper values