in reply to Re^3: Opinions on adding functionality to Tie::Hash::Sorted
in thread Opinions on adding functionality to Tie::Hash::Sorted
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:
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.#!/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"; }
Cheers - L~R
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Opinions on adding functionality to Tie::Hash::Sorted
by gaal (Parson) on Jul 18, 2004 at 15:35 UTC |