in reply to hash and while

prashantpal84:

You've already gotten some great information about the problem and ways to fix it. If you want to make the minimum changes to your program, you can take advantage of the fact that the number of keys in your hash doesn't change during your loop. So you can evaluate it once and then store it in a temporary variable for use during your loop, like so:

%hash = (a=>1,b=>2); $count = 0; $hash_key_count = keys(%hash); while(($key,$value) = each %hash) { $count++ if $value==$hash_key_count; } print $count;

This way, you don't reset the each iterator each time through the while loop.

...roboticus