in reply to hash iteration with each

if you replace

while (my ($key, $value) = each %hash) {
with
foreach my $key (keys %hash) { my $value=$hash{$key};
, it will behave as you want, without risk of nasty side-effects.

(Which begs an efficiency question. Unfortunately, I'm not qualified to answer that one.)

--
Tommy
Too stupid to live.
Too stubborn to die.