develooper Front page | perl.perl5.porters | Postings from March 2013

Re: Perlfunc for each(), keys(), values() has been changed

Thread Previous | Thread Next
From:
demerphq
Date:
March 30, 2013 16:58
Subject:
Re: Perlfunc for each(), keys(), values() has been changed
Message ID:
CANgJU+V6zc2pquVwdh7i+4ZJCt1hixs=k-TUX=sMAy3Q9dJXMw@mail.gmail.com
On 29 March 2013 19:05, Uri Guttman <uri@stemsystems.com> wrote:
> On 03/29/2013 01:46 PM, demerphq wrote:
>
>> ps: Dont worry, i have no plan of making keys annoying to use, but if
>> I can I would like to make for (keys %hash) as fast as possible!
>>
>
> what is wrong with?
>
>         while( my $key = each %hash )
>
> same effect as the optimized foreach loop other than using a named var
> (which i would do with foreach anyway).

The major objection to each() is the shared state of the iterator. I
rarely see people guard each() traversal with a reset using keys().

So making keys() as efficient to each() but without the dangers seems
like a good thing.

> now i usually do each with a key/val pair and then the empty list will be
> the boolean stop. the while $key needs a defined as '' or '0' are legal keys
> but false.

Good catch. Thanks

> still no reason to add an optimized for keys.

"make perl go faster" seems to me to be a pretty good reason all by itself. :-)

Yves

-- 
perl -Mre=debug -e "/just|another|perl|hacker/"

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About