I remember something about not changing the keys in a
for my $key ( keys %hash ) { ...
for example
for my $key ( keys %hash
The way to do this would be
$hash{$newkey} = delete $hash{$oldkey};