in reply to is there a more simple way to have the intersection of two lists ?

If neither list contains duplicate elements:
my %seen; my @intersection = grep ++$seen{$_}==2, @list1, @list2;
Or if they could
my %seen; $seen{$_}=1 for @list1; my @intersection = grep ++$seen{$_}==2, @list2;

Update: $seen{}$seen{$_}

Replies are listed 'Best First'.
Re^2: is there a more simple way to have the intersection of two lists ?
by ysth (Canon) on Mar 19, 2008 at 06:40 UTC
    my %seen; $seen{$_}=1 for @list1; my @intersection = grep ++$seen{}==2, @list2;
    You are missing a $_ there. And it doesn't work. (Consider @list1=(); @list2=qw(foo foo);.)

      Hum, right. I over-optimized. I should have stuck to the general solution (i.e. accepts more than two sets) I first wrote.

      my %seen; $seen{$_}|=1<<0 for @list1; $seen{$_}|=1<<1 for @list2; my @intersection = grep $seen{$_}==(1<<2)-1, keys %seen;

      It loses any order, though.