[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: pairs(t, skey) and ipairs(t, skey)
- From: Pierre-Yves Gérardy <pygy79@...>
- Date: Tue, 1 Oct 2013 01:17:27 +0200
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