gaal,
seeing as there's no natural way to express that for unordered hashes in Perl.
The iterator is reset anytime there is a new call to keys, or values. each will only do this once it reaches the end of the hash. This can cause very unexpected behavior if you mix and match - see the following:
#!/usr/bin/perl
use strict;
use warnings;
my %hash = (one=>1, two=>2, three=>3, four=>4, five=>5);
my $flag; # prevent infinite loop
while ( my ($key, $val) = each %hash ) {
if ( $val == 4 && ! $flag ) {
my @keys = keys %hash;
print "\t$_\n" for @keys;
$flag = 1;
}
print "$key : $val\n";
}
There very well may be some good in saving off the iterator, resetting it, and restoring it so this doesn't go bonkers as it does with real hashes. I will have to give some thought to this though.
Modified wording with regards to resetting iterator as pointed out by davido. |