On an unrelated note: use delete to remove items from a hash. If you just set them to undef, the hash keys stay (and they will be present in any loop through the hash):
use warnings;
use strict;
my %a;
$a{a} = undef;
$a{b} = 1;
print keys %a, "\n"; # prints 'ab'
delete $a{b};
print keys %a, "\n"; # prints 'a'