my %seen; while (my $key = each %hash) { next if $seen{$key}++; if ( condition($key) ) { delete @hash{ dependent_keys($key) }; keys %hash; } }