<div class="gmail_quote"><div>Hi all,</div><div><br></div><div>I've got a Perl problem that's got me stumped. Any help would be appreciated.</div><div><br></div><div>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 style="font-family:Arial, Helvetica, Georgia, sans-serif;font-size:12px;color:rgb(51, 51, 51);line-height:18px">“<span style="font-family:Consolas, Monaco, 'Courier New', Courier, monospace;line-height:18px;font-size:12px;white-space:pre"><span color=â€greenâ€>1<span></span>†is abbreviated as “<span style="font-family:Consolas, Monaco, 'Courier New', Courier, monospace;line-height:18px;font-size:12px;white-space:pre">sg1s</span>“:</span></div>
<div><span style="font-family:Arial, Helvetica, Georgia, sans-serif;font-size:12px;color:rgb(51, 51, 51);line-height:18px"><pre>#! /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);
$i=$j=0;
$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";</pre><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:10px;padding-right:0px;padding-bottom:0px;padding-left:0px">The output of this program is unfortunately:</p>
<pre>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</pre><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:10px;padding-right:0px;padding-bottom:0px;padding-left:0px">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).</p>
<p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:10px;padding-right:0px;padding-bottom:0px;padding-left:0px">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 <em>left</em> side to compile <em>multiple</em> 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.</p>
<p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:10px;padding-right:0px;padding-bottom:0px;padding-left:0px">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...)"</p>
<p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:10px;padding-right:0px;padding-bottom:0px;padding-left:0px">thx for thinking for me!</p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding-top:10px;padding-right:0px;padding-bottom:0px;padding-left:0px">
r</p></span></div><font color="#888888"><div><br></div><div><br></div>
</font></div><br>