in reply to Re: Replace zero-width grouping?
in thread Replace zero-width grouping?

I don't think that your second varient works?

The problem being that your pos() which is looking at the pos of $_, but you are applying the s/// to an substr lvalue, which is a different animal all together?.

I could be wrong on this, but I can't seem to get it to work.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Replies are listed 'Best First'.
Re: Re: Re: Replace zero-width grouping?
by diotalevi (Canon) on May 07, 2003 at 16:58 UTC

    I tested the code before posting it. It works correctly. It turns out that it works incredibly inefficiently - the longer test string shows that this loops many more times than necessary. I accidentally had the substr() falling off the end and so it had to restart a lot. I've fixed it up and now its super-slick. The substr() always starts right after the position replaced by the 'A'. This will walk the input string and never covers the same ground twice.

    # The instrumented version my $a = "17341234173412341734123417341234"; $expected = "A7AAB2BBA7AAB2BBA7AAB2BBA7AAB2BB"; $r = 0; 1 while substr($a,$r) =~ s/(?{print ">".pos()."$r\n"})(.)(...)\1(?{$r+ +=pos()-4;print"#$r\n"})/A$2B/; print "$a\n$expected\n"; print $a eq $expected ? "Ok\n" : "Failed\n"; # The clean version my $a = "17341234173412341734123417341234"; $r = 0; 1 while substr($a,$r) =~ s/(.)(...)\1(?{$r+=pos()-4;})/A$2B/; print "$a\n"