http://www.perlmonks.org?node_id=568934


in reply to What I Most Recently Learned in Perl, But Should Have Already Known

Map and grep. Especially for set functions, e.g. (untested code, but that should give the idea)
my @a=(1,2,3,4,5); my @b=(4,5,6,7,8); my %a = map { $_ => 1 } @a; my %b = map { $_ => 1 } @b; my @intersection = grep { exists $a{$_} } @b; my @intersection_complement = grep { !exists $a{$_} } @b;

Replies are listed 'Best First'.
Re^2: What I Most Recently Learned in Perl, But Should Have Already Known
by pKai (Priest) on Aug 23, 2006 at 08:49 UTC

    That reminds me that some time back I tried to get a grip on hash slices.

    In similar use to your example:

    my @intersection = grep {defined} @{{map {$_=>$_} @a}}{@b}

    Though in everyday life I prefer your method, since I tend to get headache from thinking about that slices and also definitely got the syntax and/or semantics wrong the first time always.