lua-users home
lua-l archive

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


if not next(t) then ... end

if next(t) == nil then ... end

I think those two statements work the same.
(I'm open to correction..)

No, they're different. As Mike points out: false is a valid key value, so iteration would stop at the "first" (there's no strict ordering) such key in a table.

Try this:

	t = { [false] = "aap" }

	if not next(t) then print "test1: empty!" end
	if next(t) == nil then print "test2: empty!" end

Even so, I think they will only work till the next nil element in the table,

The point was to check for an empty table. Anyway, next will iterate over all pairs. In fact:

	for k, v in pairs(t) do ... end

is completely equivalent with:

	for k, v in next, t do ... end

--
Wim