Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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.


Comment on Re: counting pairwise incidences
Download Code
Re^2: counting pairwise incidences
by LanX (Canon) 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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://905271]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-09-18 02:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (104 votes), past polls