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

Re: Getting the intersection of n collections.

by japhy (Canon)
on Oct 11, 2006 at 12:08 UTC ( #577593=note: print w/replies, xml ) Need Help??

in reply to Getting the intersection of n collections.

I'd probably start by setting one array as the resulting intersection list, and comparing each array to that one.
my @arrays = ([...], [...], [...]); my %tmp_int; # assume intersection is entirety of first array # and set each "frequency" to the number of arrays @tmp_int{@{ $arrays[0] }) = (@arrays+0) x @{ $arrays[0] }; # then check the other arrays against it for my $set (@arrays[1..$#arrays]) { my %seen; $tmp_int{$_}-- for grep { !$seen{$_} } @$set; } # the ACTUAL intersection is all the # keys in the hash whose value is 1 my @intersection = grep { $tmp_int{$_} == 1 } keys %tmp_int;

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://577593]
[1nickt]: Ah not me, good!

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2018-01-23 13:10 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (246 votes). Check out past polls.