my %in_subset = map { $_ => 1 } @subset; # Set the value 1 for each key in @subset #### 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 #### my %index_map = map { $array[$_] => $_ } 0..$#array; # Associate each element with its index (only works for unique values though) my @matching_indexes = map { $index_map{$_} } @subset; # Get the index for each value in @subset