Well, I've been working with Perl for more than ten years - unlike most humans I haven't moved on to Ruby or even PHP - but I still learn something new now and then. What I learned is that when you are going through an array counting instances of keys, if something comes along to screw with the array, it starts looping through the damn array all over again.
Let's say you have an array called %arr with values like this
apples = 4
oranges = 7
figs = 1
bananas = 2
pineapples = 8
fish = 1
And then you loop through the array to tally up the total number of items beginning with "f". The answer is 2 but you may not get that, if you store the variable with the answer in %arr, by doing something like this
while (($key,$value = each (%arr)) {
if ($key =~ /^f/) {
$arr{f}++;}}
Why? Because if the array is changed while it is looping, apparently, it starts looping all over again from the beginning and figs or fish or both are counted twice.
So you can get $arr{f} == 3 or $arr{f} == 4 or whatever.
I suppose they teach you shit like this in comp-sci but I never went there. They didn't let me in because my BO wasn't strong enough.
Anyway, lesson learned: when counting instances of items in an array, don't use the array being counted to store the tally. Capisce? Totally DUH in hindsight, but it took me a while to figure this out. It was truly a WTF??? moment when my little loop kept returning 3 instead of 2.
I kept hammering my head against the wall screaming WHY IS IT TWO?? AND WHY ARE FISH IN WITH THE FRUIT?