Theres a section in
Mastering Algorithms with Perl on sets also. If you need a full implementation of unions/differences/intersections etc see the Set::Scalar module.
If you use
chromatic's code linked to above, I gave into my compulsion to do a tiny bit of benchmarking:
my @kansas = qw( corn wheat hay cattle );
my @oklahoma = qw( wheat dairy cattle );
use Benchmark;
timethese(100000, {
"diff1" => sub { diff1(\@kansas, \@oklahoma) },
"diff2" => sub { diff2(\@kansas, \@oklahoma) },
});
sub diff1 {
my %hash;
@hash{@{$_[1]}} = (1) x @{$_[1]};
return grep { !defined $hash{$_} } @{$_[0]};
}
sub diff2 {
my %hash = map{ $_=>1} @{$_[1]};
return grep { !defined $hash{$_} } @{$_[0]};
}
It seems that a hash slice is a teeny bit faster than map for this purpose:
Results:
Benchmark: timing 100000 iterations of diff1, diff2...
diff1: 2 wallclock secs ( 1.74 usr + 0.01 sys = 1.75 CPU)
diff2: 3 wallclock secs ( 2.22 usr + 0.01 sys = 2.23 CPU)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.