[plug] Perl: Intersection of two arrays?

Simon Newton newtons at iinet.net
Tue Jun 24 17:46:24 WST 2003


Use a hash instead of the nested for loop:
http://www.perldoc.com/perl5.6/pod/perlfaq4.html#How-do-I-compute-the-difference-of-two-arrays---How-do-I-compute-the-intersection-of-two-arrays-


On Tue, 2003-06-24 at 17:23, Trevor Phillips wrote:
> Given two arrays (in Perl) with string values, I need to derive an array of 
> the elements in both. I'd like to do this quickly, too. ^_^
> 
> I know I could do it using something like:
> 
>    @intersect = grep { $s1=$_; ((grep { $s1 eq $_ } @set2)?1:0) } @set1;
> 
> ... but is there any Perl routine or popular third-party module with a nice 
> fast C equivalent version which would be faster?
> 
> Yes, I've looked through the Perl manpage and CPAN, etc... Closest I found was 
> complex OO objects & methods to do almost this. Just wondering if I missed 
> something else that was lean & useable...



More information about the plug mailing list