Because each iterates over each key and value and keys and values reset that iterator back to the beginning.
You want to get the value of keys before you iterate through the hash with each:
my %hash = qw( a 1 b 2 ); my $count = 0; my $key_count = keys %hash; while ( my ( $key, $value ) = each %hash ) { $count++ if $value == $key_count; } print $count;
In reply to Re: hash and while
by jwkrahn
in thread hash and while
by prashantpal84
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |