[San-Diego-pm] Recompiling global substitution RE?
ruberad at gmail.com
Mon Mar 22 11:07:54 PDT 2010
I have two ways to solve the problem now. If you're interested, you can
fetch a demo script operating on a more realistic (but still fake) example
Thanks for all your help!
On Fri, Mar 19, 2010 at 10:14 PM, Reuben Settergren <ruberad at gmail.com>wrote:
> Hi all,
> I've got a Perl problem that's got me stumped. Any help would be
> Consider the following small perl program, which exemplifies a problem I am
> having with a script generates HTML sw verification reports, and is supposed
> to colorize numbers according to how they verified against what the numbers
> are supposed to be. To make things simpler, “<span color=”green”>1<span>”
> is abbreviated as “sg1s“:
> #! /bin/perl
> $s = "1 2 3 1 2 3 1 2 3"; # what I have
> $t = "sg1s 2 3 sr1s 2 3 1 sy2s 3"; # what I want
> @nums = qw(1 1 2);
> @cols = qw(g r y);
> $s =~ s!($nums[$i++])!s$cols[$j++]$1s!g;
> print "Want: '$t'\n";
> print "Got: '$s'\n";
> print "i is now $i\n";
> print "j is now $j\n";
> The output of this program is unfortunately:
> Want: 'sg1s 2 3 sr1s 2 3 1 sy2s 3'
> Got: 'sg1s 2 3 sr1s 2 3 sy1s 2 3'
> i is now 1
> j is now 3
> Perl ended up incorrectly coloring the third ‘1′ yellow, instead of the
> third ‘2′, because in the substitution regular expression, the left
> (matching) side was evaluated/compiled only once, while the right
> (replacement) side was evaluated/interpolated three times (as desired).
> I know that there are ways to force the left side of the RE to compile only
> once (/o modifier, qr// operator), and there are ways to force eval in the
> right side (/e and /ee modifiers — although in this case, standard
> interpolation was sufficient), but is there any way I can force the *left* side
> to compile *multiple* times? (Even within the same /g substitution?) I
> tried a few constructs using the \G (start matching from previous position)
> zero-width assertion, but only managed to get myself into infinite loops.
> I'd appreciate any help anyone can offer -- including "What an idiot, the
> RIGHT way to do that is completely different! (And here it is...)"
> thx for thinking for me!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the San-Diego-pm