Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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]
[karlgoethebier]: My perltidy adds a semicolon after .... How can i avoid this?
[LanX]: ellipse aka ... aka yadda yadda is a new command

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2017-11-18 18:11 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (277 votes). Check out past polls.