Your skill will accomplishwhat the force of many cannot PerlMonks

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

by chromatic (Archbishop)
 on Mar 29, 2000 at 01:07 UTC Need Help??

Q&A > arrays > How can I find the union/difference/intersection of two arrays? - Answer 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);

• Comment on Answer: How can I find the union/difference/intersection of two arrays?

Create A New User
Chatterbox?
 [marto]: good morning all, TCIF [gnosti]: I'm reading from ffmpeg lengths of video clips in H:M:S and needing to add them and format them back to H:M:S for feeding back to ffmpeg [marioroy]: kcott Thanks [Corion]: gnosti: I would cheat and use my approach instead of dealing with durations as objects, or a module [Corion]: You could even do the calculation directly - my \$duration = \$H * 3600+ \$M*60 + \$S; strftime '%H:%M:%S', timelocal(\$duratio n) [Corion]: (and that strftime call is largely superfluous too, converting back to H:M:S isn't hard either)

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2017-08-18 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (297 votes). Check out past polls.

Notices?