G'day FIJI42,

Biological data are typically huge. For reasons of efficiency, when dealing with this type of data, you should choose a fast solution over a slower one. Perl's string handling functions (index, substr, etc.) are measurably faster than regexes (try it yourself with Benchmark). String operators (e.g. $x eq 'some literal') will be far more efficient than an equivalent regex (e.g. $x =~ /^some literal$/).

There are times when you will need a regex. In these cases, don't give the regex more work than necessary. In "/(AA)/gi", you're asking it to pointlessly capture matches (you never use the captured data); and you're also asking for a case-insensitive match (again pointless, as all characters are uppercase). Where you are dealing with mixed-case, or unknown case, canonicalise your data once then work with that (see fc, uc, and lc).

With your specific question, index would be the best choice.

Your ultimate goal is somewhat unclear. If you want to match "AA" once in "AAA", and twice in "AAAA":

$ perl -E ' my ($x, $y, $c, $p) = qw{AxAAxAAAxAAAA AA 0 0}; $c++, $p += 2 while ($p = index $x, $y, $p) > -1; say $c; ' 4

If you want to match "AA" twice in "AAA", and thrice in "AAAA":

$ perl -E ' my ($x, $y, $c, $p) = qw{AxAAxAAAxAAAA AA 0 0}; $c++, $p++ while ($p = index $x, $y, $p) > -1; say $c; ' 6

Note that the only difference between those two is: in the first, the position ($p) is incremented by two ($p += 2); in the second, it is incremented by one ($p++).

That code is really only about quickly demonstrating the index function. It's not intended as a production-grade solution; for instance, pick better (more meaningful) variable names.

— Ken


In reply to Re: Identifying Overlapping Matches in Nucleotide Sequence by kcott
in thread Identifying Overlapping Matches in Nucleotide Sequence by FIJI42

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.