With a tiny bit of pre-processing, you can automatically process characters in pairs:

my ( $prev, %pairs ); map { $pairs{ $prev . $_}++; $prev = $_; } @genome; delete $pairs{a}, $pairs{c}, $pairs{t}, $pairs{g};

Each character of the genome is combined in turn with the previous character to form a pair, and the corresponding entry in %pairs is incremented. Then the current character is saved in $prev to be the previous character for the next time around. Of course, for the first character there is no previous character, so there will be a dummy entry in %pairs with one of the keys 'a', 'c', 't', or 'g'. So, oncec we're all done, delete those four entries ..... so what if we deelete three entrties that don't exist.

Yes, map may be a little complicated for beginners to understand, so it may deserve a brief comment---say the previous paragraph---but it's clean and simple, and short code introduces fewer opportunities for mistakes.

Update: It's poor style to use map just for the side effects, tossing aside the return values. So it might be better to expand that line into an actual loop:

my ( $prev, %pairs ); for ( @genome ) { $pairs{ $prev . $_}++; $prev = $_; }; delete $pairs{a}, $pairs{c}, $pairs{t}, $pairs{g};

--
TTTATCGGTCGTTATATAGATGTTTGCA


In reply to Re: how can I speed up this perl?? by TomDLux
in thread how can I speed up this perl?? 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.