Apart from List::Utils that others already mentioned I'd like to add that if you want to do union, intersection or difference you should most likely be using a hash, not an array anyway. A hash with empty values, but a hash nevertheless.
# is element $a in the set?
exists $set{$a}
# remove $a from the set
delete $set{$a};
# add $a to the set
$set{$a} = undef;
# get the list of elements in the set
keys %set
# create the union
%union = (%set1, %set2);
# create the intersection
for (keys %set1) {
$intersect{$_} = undef if exists $set2{$_};
}
# create the difference
for (keys %set1) {
$diff{$_} = undef unless exists $set2{$_};
}
for (keys %set2) {
$diff{$_} = undef unless exists $set1{$_};
}
This will be much quicker than anything you could build with arrays.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|