A fly in the ointment was in the code for our $s (@stops). The code wouldn't work here with a 'my' declaration. 'our' was necessary.

This is a bug that was corrected in Perl version 5.18 IIRC. With this correction, lexical variables always work as expected in  "(?{ code })" and  "(??{ code })" regex constructs.

The dynamic regex form was necessary because the count of the quantifier changed for each iteration of the 'for' loop ($s-1).

I don't see the necessity here. Except for the fact that aliasing into the  @stop array makes calculating the quantifier a bit tedious, it can all be written normally, given that the  s/// match regex is, by default, re-compiled on each  s/// execution:

c:\@Work\Perl\monks>perl -wMstrict -le "my @stops = (2,6); ;; my $tag = '___'; ;; for ('ATCGGATCTGGC', 'A-C-G--CTGGC') { my $seq = $_; printf qq{'$seq' -> }; ;; for our $s (@stops) { local our $q = $s - 1; $seq =~ s/ ((?:[TAGC][^TAGC]*){$q} [TAGC]) /$1$tag/x; } print qq{'$seq'}; } " 'ATCGGATCTGGC' -> 'AT___CGGA___TCTGGC' 'A-C-G--CTGGC' -> 'A-C___-G--CTG___GC'
And except for say, it works under Perl version 5.8.9. See also Re: Regex to match range of characters broken by dashes Update 2 for another for-loop example.

Update: I've based my code example on your original code, prior to adding the second  s/// fixup.


Give a man a fish:  <%-{-{-{-<


In reply to Re^2: Regex to match range of characters broken by dashes by AnomalousMonk
in thread Regex to match range of characters broken by dashes by Q.and

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.