my @filtered = grep { exists $in_subset{$_} } @array; # grep for only the elements of @array which are in subset my @same = grep { $in_subset{$_} } @array; # Technically also works because trying to access a non existing key will return undef, which is false #Edited (missing } on the second line) thanks to AnomalousMonk