in reply to code that fails to act as expected...
"perldoc -f each" gives you the explanation:
When called in list context, returns a 2-element list consisting of the key and value for the next element of a hash, so that you can iterate over it. When called in scalar context, returns only the key for the next element in the hash.
(hmmm ... http://perldoc.perl.org appears to still be down)
For example:
#!/usr/local/bin/perl use strict; use warnings; my %couples = ( george => 'gracie', abbot => 'costello', johnson => 'boswell', ); my( $key, $val ); # scalar context - return keys one at a time $key = each %couples; print "$key : $couples{$key}\n"; $key = each %couples; print "$key : $couples{$key}\n"; $key = each %couples; print "$key : $couples{$key}\n"; # reset each's internal iterator $key = each %couples; # list context while ( ($key, $val) = each %couples ) { print "$key - $val\n"; }
|
|---|