Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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

by chromatic (Archbishop)
on Mar 29, 2000 at 01:07 UTC ( #6368=note: print w/replies, xml ) Need Help??


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

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);
  • Comment on Re: How can I find the union/difference/intersection of two arrays?
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://6368]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2022-01-16 21:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (50 votes). Check out past polls.

    Notices?