Re: counting pairwise incidences

by moritz (Cardinal)
 on May 17, 2011 at 14:03 UTC ( #905271=note: print w/replies, xml ) Need Help??

in reply to counting pairwise incidences

When you count strings, think of a hash.

I'm not sure if I understood what pairs you want counted. If a line has the entries a, b, c, d, do you want the pairs ab, ac, ad, bc, bd, cd?

If yes, this code should do what you want:

```use strict;
use warnings;

my %pairs;
while (<DATA>) {
my @names = split;
for my \$first (0..\$#names - 1) {
for my \$second (\$first + 1 .. \$#names) {
my \$p = join '_', @names[\$first, \$second];
\$pairs{\$p}++;
}
}
}
while (my (\$k, \$v) = each %pairs) {
print "\$k: \$v\n";
}

__DATA__
tomD gly4 phil
aesG tomD gly4 phil
aesG phil
aesG tomD gly4

If not, you need to explain what pairs you want to count.

Replies are listed 'Best First'.
Re^2: counting pairwise incidences
by ikegami (Pope) on May 17, 2011 at 17:26 UTC
Looks like ab ac ad to me, in which case it simplifies to:
```use strict;
use warnings;

my %pairs;
while (<DATA>) {
my (\$first, @names) = split;
for my \$second (@names) {
my \$p = join '_', \$first, \$second;
++\$pairs{\$p};
}
}
while (my (\$k, \$v) = each %pairs) {
print "\$k: \$v\n";
}

__DATA__
tomD gly4 phil
aesG tomD gly4 phil
aesG phil
aesG tomD gly4
Re^2: counting pairwise incidences
by LanX (Chancellor) on May 17, 2011 at 14:52 UTC
IMHO you should sort @names.

apart from this it's practically the same algorith I use. :)

Cheers Rolf

Create A New User
Node Status?
node history
Node Type: note [id://905271]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2017-06-24 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (556 votes). Check out past polls.