Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have an array containing a set of strings; all strings are of equal length but the length is variable dependent upon user argument. As an example:
AABBC BAABC AABBD AACBB
I want to calculate the frequency of each unique character present in the collection, at each position across all strings. Using the above strings, the desired result would be:
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
Does anybody know of a way to accomplish this in a succinct manner? Currently i've got this chunk of code within my script:
foreach my $segment (@collection) { foreach $i (0..length($segment)) { if (substr($segment,$i,1) eq "A") { $score[$i][1] = 1; $score[$i][2] = 0; $score[$i][3] = 0; $score[$i][4] = 0; } elsif (substr($segment,$i,1) eq "B") { $score[$i][1] = 0; $score[$i][2] = 1; $score[$i][3] = 0; $score[$i][4] = 0; } elsif (substr($segment,$i,1) eq "C") { $score[$i][1] = 0; $score[$i][2] = 0; $score[$i][3] = 1; $score[$i][4] = 0; } elsif (substr($segment,$i,1) eq "D") { $score[$i][1] = 0; $score[$i][2] = 0; $score[$i][3] = 0; $score[$i][4] = 1; } } }
Which is a very long-winded and literal way of thinking about scoring each character (before an average would be taken). I would also need to push the result each time so that multiple segments (strings) could be accommodated - but i'm not sure how and would prefer to find a better way of doing this..
|
---|