in reply to Infinite loop with each
each is inherently fragile since it uses an iterator you don't control. That's why I prefer using keys.
for my $key ( keys %some_hash ) { my $val = $some_hash{$key}; ... }
For example, each doesn't work with last or die.
>perl -le"%h=qw(a ! b @); for(1..2){ for$k(keys%h){ print ++$i,$k; las +t } }" 1a 2a >perl -le"%h=qw(a ! b @); for(1..2){ while($k=each%h){ print ++$i,$k; +last } }" 1a 2b <-- b???
Update: Added example.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Infinite loop with each
by dragonchild (Archbishop) on Jun 05, 2008 at 13:32 UTC |