use strict; use warnings; my $anchor = 1; # This is the basic element of the pairs we are lookin +g for my %results; # The final results will be found in this hash foreach (<DATA>) { my %hash; foreach (split) { # Put the array into a hash $hash{$_}=1; } if ($hash{$anchor}) { # Only process the hash if the anchor is pre +sent foreach (keys %hash) { # increment the frequencies in the resu +lts-hash ++$results{$_} unless $_==$anchor; # but omit the anchor o +r the sort will not work! } } } for my $x(sort {return $results{$b} <=> $results{$a}} keys %results) { print "$x = $results{$x}\n"; } __DATA__ 2 4 5 7 8 10 1 2 5 6 7 9 2 6 7 8 9 10 1 3 5 10 1 3 4 5 6 8 9 1 2 4 6 1 2 4 5 7 10 1 3 4 6 7 8 9
This solution only reads in each array once one at a time (low memory use and linear time for processing) and only processes the arrays where the anchor element of the pairs we are looking for is present (should be reasonably fast: on my system it processes 10000 arrays with max. 30 elements each in less than 2 seconds).
It outputs a sorted list of the number of times each element is found together with the array-element in the same array. Getting the top K-elements out of the %results-hash is left as an exercise for the reader.
CountZero
"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
In reply to Re: Best Pairs
by CountZero
in thread Best Pairs
by artist
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |