lua-users home
lua-l archive

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


steve donovan wrote:

> You are right - one might get #t==3, but luajit might disagree, etc.
> It is undefined behaviour.

Yes.  Though I do like that implementation of table length (in Lua
Java?) that was posted recently.

-----------------------------------------

Just as a reminder, depending on your requirements, you might want to
use a sentinel value instead of nil.

my_nil = {}  -- each table is a unique object

t:insert(1)
t:insert(my_nil)
t:insert(3)
t:insert(my_nil)
t:insert(5)

-- table t now has two 'holes' at indices 2 and 4.

assert(#t == 5)  -- will be true for all Lua implementations

for i, v in ipairs(t) do
	if v ~= my_nil then
		print(i, v) -- just print the actual values.
	end
end


James Graves