Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How can I find the union/difference/intersection of two arrays?

( #2461=categorized question: print w/ replies, xml ) Need Help??
Contributed by vroom on Jan 26, 2000 at 00:58 UTC
Q&A  > arrays


Answer: How can I find the union/difference/intersection of two arrays?
contributed by chromatic

This version works. :)

my @simpsons=("homer","bart","marge","maggie","lisa"); my @females=("lisa","marge","maggie","maude"); my %simpsons=map{$_ =>1} @simpsons; my %females=map{$_=>1} @females; # the intersection of @females and @simpsons: my @female_simpsons = grep( $simpsons{$_}, @females ); # proof it works print "Female Simpson:\t$_\n" foreach (@female_simpsons); # the difference of @females and @simpsons my @male_simpsons=grep(!defined $females{$_}, @simpsons); # proof it works print "Male Simpson:\t$_\n" foreach (@male_simpsons); my %union = (); # the union of @females and @simpsons foreach(@females,@simpsons){ $union{$_}=1; } my @union2 = keys %union; # or just do this # my @union = (@females, @simpsons);
Answer: How can I find the union/difference/intersection of two arrays?
contributed by DrHyde

TheDamian's Quantum::Superpositions module can do this. Although it is slow, the code is quite readable:

use Quantum::Superpositions; my @a = (1,2,3,4,5,6,7,8,9,10); # integers my @b = (2,4,6,8,10,12,14,16,18,20); # doubled my @unionAB = sort { $a <=> $b } eigenstates( any(@a, @b) ); my @intersectionAB = sort { $a <=> $b } eigenstates( any(@a) == any(@b +) ); my @differenceAB = sort { $a <=> $b } eigenstates( any(@a) != all(@b +) );
Answer: How can I find the union/difference/intersection of two arrays?
contributed by McMahon

The List::Compare module has functions for all of these operations.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (11)
    As of 2014-07-28 11:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (196 votes), past polls