Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Tallying overall frequency of characters in a set of strings by position

by johngg (Canon)
on May 11, 2016 at 23:48 UTC ( [id://1162820]=note: print w/replies, xml ) Need Help??


in reply to Tallying overall frequency of characters in a set of strings by position

Here's a more long-winded approach that seems to work for extra letters/rows.

use strict; use warnings; my @strings = qw{ AABABC BAABEC AABFBD AACBDB CBBDEF }; my $div = scalar @strings; my @stringAoA = map { [ split m{} ] } @strings; my %letters; $letters{ $_ } ++ for map { @{ $_ } } @stringAoA; my %scores; for my $posn ( 1 .. length $strings[ 0 ] ) { for my $row ( 0 .. $#stringAoA ) { $scores{ $posn }->{ $stringAoA[ $row ]->[ $posn - 1 ] } ++; } } printf qq{%8s@{ [ q{%8s} x scalar keys %letters ] }\n}, q{}, sort keys %letters; for my $posn (sort { $a <=> $b } keys %scores ) { printf qq{ %8d@{ [ q{%8.2f} x scalar keys %letters ] }\n}, $posn, map { defined $scores{ $posn }->{ $_ } ? $scores{ $posn }->{ $_ } / $div : 0 } sort keys %letters }

The output.

A B C D E F 1 0.60 0.20 0.20 0.00 0.00 0.00 2 0.80 0.20 0.00 0.00 0.00 0.00 3 0.20 0.60 0.20 0.00 0.00 0.00 4 0.20 0.40 0.00 0.20 0.00 0.20 5 0.00 0.40 0.00 0.20 0.40 0.00 6 0.00 0.20 0.40 0.20 0.00 0.20

I hope this is useful.

Cheers,

JohnGG

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-28 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found