my %hash = map{$_=>undef} @inputs; foreach my $k (keys %hash){ # do something delete $hash{$k} if $do_not_keep; } # then do this loop again...