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

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

Thread Previous | Thread Next
From:
Eric Brine
Date:
March 31, 2013 02:14
Subject:
Re: Perlfunc for each(), keys(), values() has been changed
Message ID:
CALJW-qH4frdUW4CEVknfx35hArJ+aQ_i2LSqFHhqfG2Cp-N4iw@mail.gmail.com
On Sat, Mar 30, 2013 at 12:12 PM, Brad Gilbert <b2gills@gmail.com> wrote:

> The text states that you can delete the current key during iteration,
> without changing the order.
>

> What it should say is that it won't affect the order until after the
> iteration is done.
>

The first delete in

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

happens after the iteration is done. Do you and demerphq not realise that
the above snippet is the same as

my @keys = keys(%hash);
foreach my $key (@keys) {
  delete $hash{$key} if cond();
}

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