Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Compare two arrays for uniqueness

by Kenosis (Priest)
on Dec 16, 2013 at 04:51 UTC ( #1067285=note: print w/replies, xml ) Need Help??


in reply to Compare two arrays for uniqueness

The module List::Compare lives for this (and a few other things):

use strict; use warnings; use List::Compare; my @mammals = qw/dog cat mouse horse planet/; my @sky = qw/star planet galaxy nova comet/; my $lc = List::Compare->new( \@mammals, \@sky ); my @intersection = $lc->get_intersection; print "@intersection", "\n"; # prints "planet" print scalar @intersection, "\n"; # prints 1 my @arr1 = 0 .. 5; my @arr2 = 6 .. 10; $lc = List::Compare->new( \@arr1, \@arr2 ); @intersection = $lc->get_intersection; print "@intersection", "\n"; # prints "" print scalar @intersection, "\n"; # prints 0