lua-users home
lua-l archive

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



Hey, this is fun.. :)

	-- Bug hunt!
	--
	local tbl= { 'a','b',nil,'c' }
	print( table.getn(tbl) )    -- 4 (w1) / 2 (w0)
	tbl[4]= nil
	print( table.getn(tbl) )    -- 2
	tbl[5]= { 'more..' }
	print( table.getn(tbl) )    -- 2
	tbl[4]= nil
	print( table.getn(tbl) )    -- 2
	tbl[5]= nil
	print( table.getn(tbl) )    -- 2

Seems the semantics are like.. *guessing* if the 'nil's are there in the _first place_, they count. If they arrive later, due to dynamic changes, they don't. If this is the case, why bother with 'table.getn' at all? Just use 'random'!

-ak


2.9.2004 kello 15:12, Adam D. Moss kirjoitti:

 Adam D. Moss wrote:
Oh, sorry, I misread your code a bit.  What I mentioned would
explain the 2,nil thing but perhaps not the (n>0 and (not tbl[n]))
thing (unless tbl[n] was boolean false).

Gah, third try.  Don't mind me, it was a heavy night last
night... I do see your main point, that the getn() of an
apparently-empty table is !0.  I wonder if this is related
to the very few cases where Lua makes a distiction between
table indices that are truly empty and table indices that
have had nil explicitly assigned to them.

--Adam