Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Getting the intersection of n collections.

by basje (Beadle)
on Oct 11, 2006 at 12:30 UTC ( #577595=note: print w/ replies, xml ) Need Help??


in reply to Getting the intersection of n collections.

this also works with duplicates

my $sets = [ [1,2,3,3,3,3,4,5,6,7,8,9,10], [1,1,1,2,6,7], [3,4,5,5,5,5,6,6,6,6,7,8,9], ]; my %hash = map { $_, 1 } @{$sets->[0]}; for(my $i=1; $i<scalar(@{$sets});$i++) { %hash = map { $_, 1 } grep {%hash->{$_}} @{$sets->[$i]}; } return (keys(%hash));


Comment on Re: Getting the intersection of n collections.
Download Code
Replies are listed 'Best First'.
Re^2: Getting the intersection of n collections.
by johngg (Abbot) on Oct 11, 2006 at 13:58 UTC
    I think you may have a typo in your solution. The grep {%hash->{$_}} should perhaps be grep {$hash{$_}}.

    Nice solution, though.

    Cheers,

    JohnGG

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2015-07-29 11:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls