I figured out what the main problem is.

Correct me if I'm wrong, but the s/// leaves off at the position just after the last replacement ($+[0]). So, if you have

$str = q{TTTTTTT}; $str =~ s/TTT/ttt/i;
the results is $str = tttTTTT But, if you throw a /g on the end of that, you'll have: $str = ttttttT

The problem I've been having is that rather than having s/// leave off at $+[0], I was assuming it left off at $-[0] + 1. The difference being, in the example above, with the /g included, the results would be:

$str = tttTTTT # after the first replacement $str = ttttTTT # after the second replacement .... $str = ttttttt # after all the global matches are said and done
This is what I had been hoping for.

So, my question becomes, is there a way to set s/// to start looking for replacements at $-[0] + 1 when in a global context, rather than at $+[0]


In reply to Re: s/// only replacing most of the time... by mdunnbass
in thread s/// only replacing most of the time... by mdunnbass

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.