in reply to 1 while s/// vs s///g

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!

Replies are listed 'Best First'.
Re: Re: 1 while s/// vs s///g
by merlyn (Sage) on May 23, 2001 at 23:54 UTC
    Was there deeper/more subtle meaning that I missed out on?
    Go forth, my son, and share your newfound knowledge with friends and loved ones.

    -- Randal L. Schwartz, Perl hacker

Pumping Lemma
by John M. Dlugosz (Monsignor) on May 24, 2001 at 01:35 UTC
    I had a strange vision of pumping lemmings when I read that...

    Seriously, the while loop is also different (doesn't matter here but does in more real code where a s//g is the condition of a while with a non-empty body) because without the /g modifier the pos will not be set and /G doesn't work.

    —John