no·mol·o·gy n.
The study and discovery of general physical and logical laws.

Friday, April 3, 2009

Set intersection using hash slices

my @colours = qw/red green yellow orange white mauve blue ochre
                 pink purple gold silver grey brown steel/;
my @find = qw/red blue green black/;
my %colours;
@colours{@colours} = @colours;
my @found = grep { defined } @colours{@find};
print join( ', ', @found );

This prints "red, blue, green". Adapted from a Perl.sig Perl Tip on slicing arrays and hashes.