Merlyn-sama,

gregor42 bows humbly...

Do I grok your intention or am I to go back to meditation? .. :

1 while s/$string//;

Will 'collapse' the string:

"aaaaabbbbb"

From the middle... As each centre pair of "ab" are 'removed'(replaced will nil) the regex starts over completely and repeats the process. An extra 'a' on the left or an extra 'b' on the right of that string would be left over at the end of the operation.

INPUT: aaaaabbbbb OUTPUT:


Whereas:

s/$string//g;

Though looking at the string repeatedly, it does it's matching against only the first string and not the string subsequent to substitution. Hence, only the centre pair are removed and the rest left over. The added 'a' or 'b' on the left & right respectively, if added singly or doubly would not change how often the pattern is matched & would still be present with the rest of the characters in the string.

INPUT: aaaaabbbbb OUTPUT: aaaabbbb


Most thought provoking.... Thank you once again for sharing with those of us with lesser brain.

Was there deeper/more subtle meaning that I missed out on?



Wait! This isn't a Parachute, this is a Backpack!

In reply to Re: 1 while s/// vs s///g by gregor42
in thread 1 while s/// vs s///g by merlyn

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.