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

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

Thread Previous | Thread Next
Uri Guttman
March 30, 2013 17:30
Re: Perlfunc for each(), keys(), values() has been changed
Message ID:
On 03/30/2013 12:58 PM, demerphq wrote:
> On 29 March 2013 19:05, Uri Guttman <> 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().

i agree the single iterator is an issue. but how would optimizing for 
(keys  %hash) not also have an iterator that is attached to the hash? if 
you make the iterator part of the keys, then you can do the same for 
each. the problem with multiple iterators would be if one loop modified 
the hash while doing another iteration. maybe deletes would be allowed 
there. the iterator has to know the hash guts which is why it is 
currently tied to the hash itself.

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

if you can remove the dangers. i don't see how it is different than 
while/each in that respect.

>> 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. :-)

i just use each and haven't needed nested iterations. that seems to be 
rarely needed and you can work around it with keys currently. as i said, 
if you can optimize keys, it should also work for while/each as you want 
an external iterator in both cases.



Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About