[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: pairs(t, skey) and ipairs(t, skey)
- From: Paul K <paulclinger@...>
- Date: Mon, 30 Sep 2013 17:27:15 -0700
Hi Pierre,
> 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
Indeed; I missed that in the manual. Thank you for the suggestion.
I should have used "#t > 0 and #t or nil" instead of #t to account for
empty tables.
Paul.
On Mon, Sep 30, 2013 at 4:17 PM, Pierre-Yves Gérardy <pygy79@gmail.com> wrote:
> 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
>