in reply to Using foreach to process a hash

You could use foreach to iterate over the keys or values, or you could use a while loop and each.
my %hash = ( a => 1, b => 2); while (my ($key,$val) = each %hash) { print "$key = $val\n"; } for my $key (keys %hash) { my $val = $hash{$key}; print "$key = $val\n"; }

Replies are listed 'Best First'.
Re^2: Using foreach to process a hash
by ikegami (Patriarch) on Oct 22, 2006 at 21:05 UTC

    Note: You can't safely use last, return or die to exist a while each loop. Not running each to the end makes each unusuable on that hash at a latter point. While less memory efficient, the for alternative avoids this problem.