gaal,
They would be direction switches. The reason for naming them explicitly would be so code that used them would be clear in what was going on but you are right - functionally only one is needed.
A prev and next method were supposed to be coupled with the peek methods (which would actually change the iterator), but I got trigger happy and posted before the brain dump was complete. Thanks for mopping up the grey matter.
| [reply] |
Hmmm, I just thought of another method that might be useful: rewind, to reset the iterator. But that's probably low priority, seeing as there's no natural way to express that for unordered hashes in Perl.
| [reply] |
#!/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. | [reply] [d/l] |