Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: better union of sets algorithm?

by injunjoel (Priest)
on Mar 11, 2005 at 20:14 UTC ( [id://438789]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://438789]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-26 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found