lua-users home
lua-l archive

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


Hi All,

I was looking for a way to find if there are any keys in the hash part
of a table without iterating the table and realized that I can use
"next" to give me the answer as it accepts a starting key:

next(t, #t) returns the first key in the hash part (if any).

I then tried to iterate only hash keys (lua 5.1), but it turned out
that pairs and ipairs don't accept the starting key, so I ended up
with something like this:

for k,v in (function() return next, t, #t end)() do print(k,v) end

I think "for k,v in pairs(t, #t) do print(k,v) end" would look much
better. Are there any reasons why pairs and ipairs don't accept
starting keys?

It also seems based on my cursory look at the source that lua 5.2 may
accept the starting key, but I don't see anything in the documentation
that confirms that
(http://www.lua.org/manual/5.2/manual.html#pdf-pairs).

Paul.