Like this?:
Perhaps this is closer to what you are after (Updated: corrected output ordering):
#! perl -slw use strict; use Data::Dump qw[ pp ]; my @freq; my @data = qw[ AABBC BAABC AABBD AACBB ]; for my $s ( @data ) { ++$freq[ $_ ]{ substr $s, $_, 1 } for 0 .. length( $s ) -1; } ##pp \@freq; for my $pos ( @freq ) { ( $pos->{ $_ } //= 0 ) /= 4 for 'A' .. 'D'; } ##pp \@freq; print join "\t", '', 'A'..'D'; for my $pos ( 0 .. $#freq ) { printf "%2d\t%s\n", $pos+1, join "\t", map sprintf("%.2f", $_ ), + @{ $freq[ $pos ] }{ 'A' .. 'D' }; } __DATA__ C:\test>1162755 A B C D 1 0.75 0.25 0.00 0.00 2 1.00 0.00 0.00 0.00 3 0.25 0.50 0.25 0.00 4 0.00 1.00 0.00 0.00 5 0.00 0.25 0.50 0.25
Another variations that doesn't hard code the keys:
#! perl -slw use strict; use Data::Dump qw[ pp ]; my( @freq, %c, $c ); #my @data = qw[ AABBC BAABC AABBD AACBB ]; my @data = qw[ AABBC BAABC AABBD AECBBF ]; for my $s ( @data ) { ++$freq[ $_ ]{ $c = substr $s, $_, 1 }, undef $c{ $c } for 0 .. le +ngth( $s ) -1; } ##pp \@freq; my @oK = sort keys %c; for my $pos ( @freq ) { ( $pos->{ $_ } //= 0 ) /= 4 for @oK; } ##pp \@freq; print join "\t", '', @oK; for my $pos ( 0 .. $#freq ) { printf "%2d\t%s\n", $pos+1, join "\t", map sprintf("%.2f", $_ ), + @{ $freq[ $pos ] }{ @oK }; } __DATA__ C:\test>1162755 A B C D E F 1 0.75 0.25 0.00 0.00 0.00 0.00 2 0.75 0.00 0.00 0.00 0.25 0.00 3 0.25 0.50 0.25 0.00 0.00 0.00 4 0.00 1.00 0.00 0.00 0.00 0.00 5 0.00 0.25 0.50 0.25 0.00 0.00 6 0.00 0.00 0.00 0.00 0.00 0.25
In reply to Re: Tallying overall frequency of characters in a set of strings by position
by BrowserUk
in thread Tallying overall frequency of characters in a set of strings by position
by Anonymous Monk
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |