in reply to better union of sets algorithm?

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

Replies are listed 'Best First'.
Re^2: better union of sets algorithm?
by perrin (Chancellor) on Mar 11, 2005 at 20:27 UTC
    Thanks, but that's an intersection, not a union.