Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
go ahead... be a heretic
 
PerlMonks

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

 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

( #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
  • 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.
  • Login:
    Password
    remember me
    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 cooling their heels in the Monastery: (26)
    tye
    GrandFather
    shmem
    jdporter
    holli
    Gavin
    apl
    salva
    jaldhar
    kennethk
    thezip
    Eyck
    Perlbotics
    Marshall
    erix
    pemungkah
    splinky
    ssandv
    roubi
    Rodster001
    MikeDexter
    jonathanu
    AriSoft
    im2
    Ulix
    smetj
    As of 2010-02-09 21:54 GMT
    Sections?
    The Monastery Gates
    Seekers of Perl Wisdom
    Meditations
    PerlMonks Discussion
    Categorized Q&A
    Tutorials
    Obfuscated Code
    Perl Poetry
    Cool Uses for Perl
    Perl News
    Information?
    PerlMonks FAQ
    Guide to the Monastery
    What's New at PerlMonks
    Voting/Experience System
    Tutorials
    Reviews
    Library
    Perl FAQs
    Other Info Sources
    Find Nodes?
    Nodes You Wrote
    Super Search
    List Nodes By Users
    Newest Nodes
    Recently Active Threads
    Selected Best Nodes
    Best Nodes
    Worst Nodes
    Saints in our Book
    Leftovers?
    The St. Larry Wall Shrine
    Offering Plate
    Awards
    Craft
    Snippets Section
    Code Catacombs
    Quests
    Editor Requests
    Buy PerlMonks Gear
    PerlMonks Merchandise
    Planet Perl
    Perlsphere
    Use Perl
    Perl.com
    Perl 5 Wiki
    Perl Jobs
    Perl Mongers
    Perl Directory
    Perl documentation
    CPAN
    Random Node
    Voting Booth?

    What level of existential comfort do you require?

    Palace
    Executive suite at the best hotel
    Regular hotel in a decent part of town
    Motel
    Boarding house
    Sleeping Bag on Couch in Basement
    Any port in a storm
    Camping under the freeway overpass
    Jail
    Other

    Results (283 votes), past polls