Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Greetings all,
Final Update!
So after my first mis-reading of the question (I computed the intersection not the union) and my subsequent code offering, I figured someone out there had to have already written this functionality, and I was right! So after a little research I would suggest List::Compare.

Two week in the lab can save you two hours in the library...
Here is my suggestion:
...clipped... (not applicable) #I was computing the intersection and not the union.

of course you will need to alter $list_count depending on how many arrays you are using.


Update! Perhaps I should have read the replies in more depth since I apparently didnt understand the OP question.
In which case my suggestion would follow those already presented... in short:
my @unique = do{ my %seen; undef @seen{@list1, @list2, @list3}; sort keys %seen;};

here is the original node with benchmarks for this.

Update! This got me thinking and here is what I humbly offer to the community at large.
package ListMerger; use strict; sub new { my $pkg = shift; my $self; return bless \$self, $pkg; } #call with references to lists as arguments #ex: @union = $obj->union(\@list1,\@list2,\@list3); sub union { my $self = shift; my @lists = map{@{$_}}@_; return do{ my %seen; undef @seen{@lists}; sort keys %seen; }; } #call with references to lists as arguments sub intersection { my $self = shift; my @lists = map{@{$_}}@_; my $list_count = scalar(@_); return do{my %seen; $seen{$_}++ for(@lists); map{delete $seen{$_} if($seen{$_}<$list_count)}keys %seen; sort keys %seen; }; } #call with references to lists as arguments sub exclusion { my $self = shift; my @lists = map{@{$_}}@_; my $list_count = scalar(@_); return do{my %seen; $seen{$_}++ for(@lists); map{delete $seen{$_} if($seen{$_} == $list_count)}keys %se +en; sort keys %seen; }; } #call with references to list as arguments sub shared { my $self = shift; my @lists = map{@{$_}}@_; return do{my %seen; $seen{$_}++ for(@lists); map{delete $seen{$_} if($seen{$_} == 1)}keys %seen; sort keys %seen; }; } 1;


-InjunJoel
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

In reply to Re: better union of sets algorithm? by injunjoel
in thread better union of sets algorithm? by perrin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-03-19 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found