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

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

Thread Previous | Thread Next
From:
Brad Baxter
Date:
March 29, 2013 17:16
Subject:
Re: Perlfunc for each(), keys(), values() has been changed
Message ID:
CANBtU9bw2LeOXeJzdB+LOcP2YO5H6tzuY=JTYL6QK_q7HZ9sDA@mail.gmail.com
On Fri, Mar 29, 2013 at 1:06 PM, Aristotle Pagaltzis <pagaltzis@gmx.de>wrote:
...

> But the specific behaviour now is really quite irrelevant. (Even more so
> is defending why it got to be that way; IMO that merits mention only as
> gory details for the curious.) The point of interest to a programmer
> consulting the docs is guidance on what to *do*: how to write correct
> code; not background on what Perl does and why. IMO that is the emphasis
> with which it should be written.
>
> So how about this? Or something along these lines.
>

In the spirit of guidance on what to do, I think including the examples
below to help understand "the entry most recently returned from C<each> or
C<keys>" (particularly keys) would be helpful.

On 29 March 2013 15:19, David Golden <xdg@xdg.me> wrote:
> On Fri, Mar 29, 2013 at 7:39 AM, demerphq <demerphq@gmail.com> wrote:
...
> I don't understand what you mean by "most recent key returned by each
> or keys".  What is the most recent key returned by keys()?  Are you
> trying to say you can delete during each() without changing the order
> of subsequent keys/values returned by each()?

Well it means that

keys %hash; #reset each iterator
while (my $key= each %hash) {
  delete $hash{$key};
}

and

foreach my $key (keys %hash) {
  delete $hash{$key};
}

will never change the iteration order.

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