I am using each to iterate through a Perl hash:
while (my ($key,$val) = each %hash) {
...
}
Then something interesting happ
Let's not forget that keys %hash is already defined when you enter the while loop. One could have simply saved the keys into an array for later use:
my @keys = keys %hash;
while (my ($key,$val) = each %hash) {
if (something_interesting_happens()) {
print "$_ => $hash{$_}\n" for @keys;
}
}
Downside:
%hash is modified (but then why would one use each in the first place?)Upside: