use List::Compare; use caveat "I haven't tested this."; my @in_both = List::Compare->new([split /\s+/, $a], [split /\s+/, $b])->get_intersection;