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..


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.