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":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.