Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

comparing arrays

by Becky (Beadle)
on Apr 28, 2004 at 10:57 UTC ( #348779=perlquestion: print w/replies, xml ) Need Help??
Becky has asked for the wisdom of the Perl Monks concerning the following question:

I have 2 arrays and want to know which elements occur in both.

@array1 = (1,2,214,215,216,217,218,219,220,221);
@array2 = (214,215,216,217,218,219);

What I need to end up with is another array containing just those numbers that occur in both array1 and array2, so that I can then compare this new array with a third array and repeat the process.


Replies are listed 'Best First'.
Re: comparing arrays
by pbeckingham (Parson) on Apr 28, 2004 at 11:23 UTC

    Here is a way:

    #! /usr/bin/perl -w use strict; my @array1 = (1,2,214,215,216,217,218,219,220,221); my @array2 = (214,215,216,217,218,219); my %hash2 = map {$_ => undef} @array2; for (@array1) { print $_, "\n" if exists $hash2{$_}; }
    But as phenom says, Array::Compare is better.

Re: comparing arrays
by phenom (Chaplain) on Apr 28, 2004 at 11:03 UTC
    Try Array::Compare. Also try the super search here - I'm pretty sure this question has just come up recently.
      Come to think of it, List::Compare would probably be more appropriate. Try them both - let me know which one worked better.
Re: comparing arrays
by Art_XIV (Hermit) on Apr 28, 2004 at 12:30 UTC

    perlfaq4 contains the canonical response, although one of the ::Compare modules mentioned above may be better for your purposes.

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: comparing arrays
by edan (Curate) on Apr 28, 2004 at 14:57 UTC
Re: comparing arrays
by QM (Parson) on Apr 28, 2004 at 13:58 UTC
    perldoc -q intersection Found in C:\Perl\lib\pod\perlfaq4.pod How do I compute the difference of two arrays? How do I compute the + intersection of two arrays? Use a hash. Here's code to do both and more. It assumes th +at each element is unique in a given array: @union = @intersection = @difference = (); %count = (); foreach $element (@array1, @array2) { $count{$element} +++ } foreach $element (keys %count) { push @union, $element; push @{ $count{$element} > 1 ? \@intersection : \@ +difference }, $element; } Note that this is the *symmetric difference*, that is, all elements in either A or in B but not in both. Think of it +as an xor operation.

    Quantum Mechanics: The dreams stuff is made of

Re: comparing arrays
by McMahon (Chaplain) on Apr 28, 2004 at 15:00 UTC
Re: comparing arrays
by EdwardG (Vicar) on Apr 28, 2004 at 12:28 UTC that I can then compare this new array with a third array and repeat the process

    To determine what elements are common to a set of arrays you should use hashes, since that is the mechanism best suited to checking if values have previously been encountered.

    $h{$_}++ for (@array1,@array2); @common_elements = grep {$h{$_} > 1} keys %h; print "@common_elements";

    With a little imagination you could make this generic to operate on N arrays, not just two.

      This breaks if the one array has multiple occurences of the same elements. Of course we don't konw what the requirments are from the specs.

      Eric Hodges

        That's right, it assumes that each element is unique in a given array, just like the version in the faq.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://348779]
Approved by broquaint
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-01-21 05:51 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.