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

Re: counting pairwise incidences

by jpl (Monk)
on May 17, 2011 at 14:19 UTC ( #905277=note: print w/replies, xml ) Need Help??

in reply to counting pairwise incidences

Or, as a minor variant of the code posted by moritz
use strict; use warnings; my %pairs; while (<DATA>) { my @names = split; # my @names = sort split; # if order shouldn't matter while ( @names > 1 ) { my $first = shift(@names); map { ++$pairs{ join '_', $first, $_ } } @names; } } while ( my ( $k, $v ) = each %pairs ) { print "$k: $v\n"; } __DATA__ tomD gly4 phil aesG tomD gly4 phil aesG phil aesG tomD gly4

Replies are listed 'Best First'.
Re^2: counting pairwise incidences
by reubs85 (Acolyte) on May 19, 2011 at 13:33 UTC

    Thanks everyone for their help! The order doesn't matter so the variant of the code supplied by moritz and jpl works a treat. Highly impressed by the speed of answering btw; I just checked back on the site I never expected replies to be so quick... long live the brotherhood!



Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://905277]
and all is quiet...

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

    Results (278 votes). Check out past polls.