my @not_in_hash = grep {!exists $hash{$_}} @all_ids; my %only_in_hash = %hash; #make a copy first delete @only_in_hash{@all_ids};