Get the intersection of two lists of strings in Perl

Array::Utils is what you're looking for.

use Array::Utils qw(:all);

my @a = qw( a b c d );
my @b = qw( c d e f );

my @isect = intersect(@a, @b);
print join(",",@isect) . "\n";

This produces the expected output of

c,d

Edit: I didn't notice that you wanted this done case-insensitively. In that case, you can replace @a with map{lc}@a (and likewise with @b).


Here's one map/grep approach:

my @a = qw(Perl PHP Ruby Python C JavaScript);
my @b = qw(erlang java perl python c snobol lisp);
my @intersection =
    grep { defined }
        @{ { map { lc ,=> $_ } @a } }
           { map { lc } @b };

# @intersection = qw(Perl Python C)

Tags:

Perl