lua-users home
lua-l archive

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


On Tue, Oct 1, 2013 at 12:51 AM, Paul K <paulclinger@yahoo.com> wrote:
> 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

You don't need to wrap it in an anonymous function. The generic for
loop takes three expressions.

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

^^ This will do what you want.

-- Pierre-Yves