I have two ways to solve the problem now. If you&#39;re interested, you can fetch a demo script operating on a more realistic (but still fake) example from <a href="http://gist.github.com/340326">http://gist.github.com/340326</a>.<div>

<br></div><div>Thanks for all your help!</div><div><br></div><div>r</div><div><br><div><div><br><div class="gmail_quote">On Fri, Mar 19, 2010 at 10:14 PM, Reuben Settergren <span dir="ltr">&lt;<a href="mailto:ruberad@gmail.com">ruberad@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="gmail_quote"><div>Hi all,</div><div><br></div><div>I&#39;ve got a Perl problem that&#39;s got me stumped. Any help would be appreciated.</div>

<div><div></div><div class="h5"><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, &#39;Courier New&#39;, Courier, monospace;line-height:18px;font-size:12px;white-space:pre">&lt;span color=”green”&gt;1&lt;span&gt;</span>” is abbreviated as “<span style="font-family:Consolas, Monaco, &#39;Courier New&#39;, 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 = &quot;1 2 3 1 2 3 1 2 3&quot;;          # what I have
$t = &quot;sg1s 2 3 sr1s 2 3 1 sy2s 3&quot;; # 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 &quot;Want: &#39;$t&#39;\n&quot;;
print &quot;Got:  &#39;$s&#39;\n&quot;;
print &quot;i is now $i\n&quot;;
print &quot;j is now $j\n&quot;;</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: &#39;sg1s 2 3 sr1s 2 3 1 sy2s 3&#39;
Got:  &#39;sg1s 2 3 sr1s 2 3 sy1s 2 3&#39;
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&#39;d appreciate any help anyone can offer -- including &quot;What an idiot, the RIGHT way to do that is completely different! (And here it is...)&quot;</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></div></div><br>
</blockquote></div><br></div></div></div>