Hey gang,<div><br></div><div>I think I have a solution now, with the help of Mark Johnson. I should give you more clarification that my actual problem (still simplified) looks more like:</div><div><br></div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><div>

<font face="&#39;courier new&#39;, monospace">DIST - Distance</font></div><div><font face="&#39;courier new&#39;, monospace">    DISTANCE:   12.3456 ft    4.5678 m</font></div><div><font face="&#39;courier new&#39;, monospace">    STD DEV:     3.4567 ft    1.2345 m</font></div>

<div><font face="&#39;courier new&#39;, monospace"><br></font></div><div><font face="&#39;courier new&#39;, monospace">AREA - Area</font></div><div><font face="&#39;courier new&#39;, monospace">    AREA:      2345.6789 ft^2   345.6789 m^2</font></div>

<div><font face="&#39;courier new&#39;, monospace">    PERIMETER:  234.5678 ft      89.0123 m</font></div><div><font face="&#39;courier new&#39;, monospace">    STD DEV:      3.4567 ft       1.2345 m</font></div><div><font face="&#39;courier new&#39;, monospace"><br>

</font></div><div><font face="&#39;courier new&#39;, monospace">DIST - Distance</font></div><div><font face="&#39;courier new&#39;, monospace">    DISTANCE:   12.3456 ft   4.5678 m</font></div><div><font face="&#39;courier new&#39;, monospace">    STD DEV:     3.4567 ft    1.2345 m</font></div>

<div><font face="&#39;courier new&#39;, monospace"><br>DIST - Distance</font></div><div><font face="&#39;courier new&#39;, monospace">    DISTANCE:    8.9012 ft    3.0123 m</font></div><div><font face="&#39;courier new&#39;, monospace">    STD DEV:     1.2345 ft    0.4567 m</font></div>

<div><font face="&#39;courier new&#39;, monospace">...etc</font></div><div><br></div><div><br></div><div>So in any one situation, I will have a set of numbers/colors for a particular combination of function/metric/unit (i.e. DIST/&#39;STD DEV&#39;/ft: (3.4567,3.4567,1.2345),(red,green,yellow)). So I have to avoid the other functions (e.g. AREA) that might have the same numbers. Note my first attempt</div>

<div><font face="&#39;courier new&#39;, monospace">for $i (0..$#nums) {</font></div><div><font face="&#39;courier new&#39;, monospace">   $str =~ s{($function.*$metric.*)(?&lt;!&gt;)($vals[$i]\s+$unit)}</font></div><div>
<font face="&#39;courier new&#39;, monospace">            {$1&lt;span color=&quot;$cols[$i]&quot;&gt;$2&lt;/span&gt;}</font></div>
<div><font face="&#39;courier new&#39;, monospace">}</font></div><div>didn&#39;t work because in the second time through the loop, $1 matched from the very beginning, and $2 was the 3.4567 in the AREA function.</div><div>

<br></div><div>But with Mark Johnson&#39;s idea, I can chomp through my whole file-in-one-string bit-by-bit, something like:</div><div><br></div><div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">$head = &#39;&#39;;</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">$tail = $s;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">for my $i ( 0 .. $#nums ) {</font></div><div>

<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    $tail =~ s/(.*?$function.*?$metric.*?)($nums[$i]\s+$unit)(.*)/$3/;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    $head .= &quot;$1&lt;span color=&quot;$cols[$i]&quot;&gt;$2&lt;/span&gt;&quot;;</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">}</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">$s = &quot;$head$tail&quot;;</font></div><div><br></div>

<div>If there isn&#39;t already a name for this cool kind of trick, there should be: train cars? chewandswallow? sausage grinder? Any other ideas?</div><div><br></div><div>r</div><div><br></div></div><div><br></div></span><br>

<div class="gmail_quote">On Sat, Mar 20, 2010 at 8:41 AM, Gautam Dey <span dir="ltr">&lt;<a href="mailto:gautam.dey77@gmail.com">gautam.dey77@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="im">Reuben,<br>
<br>
   Trying to figure out how you are determining when to do the substitution?<br>
<br>
<br>
<br>
</div>2010/3/19 Reuben Settergren &lt;<a href="mailto:ruberad@gmail.com">ruberad@gmail.com</a>&gt;:<br>
&lt;snip&gt;<br>
<div class="im">&gt;<br>
&gt; #! /bin/perl<br>
&gt; $s = &quot;1 2 3 1 2 3 1 2 3&quot;;          # what I have<br>
&gt; $t = &quot;sg1s 2 3 sr1s 2 3 1 sy2s 3&quot;; # what I want<br>
<br>
</div><div class="im">How do you know that there are two elements after the first 1 to generate<br>
the &quot;sg1s 2 3&quot; and that there are three elements after the 1 to generate the<br>
&quot;sr1s 2 3 1&quot; and that there is only 1 element after the 2 to generate the<br>
&quot;sy2s 3&quot;. The transformation seem a bit more complicated then what can<br>
be done with a simple regular expresion .<br>
<br>
s/something/text/g replaces all occurrences of something with text.<br>
So, you regular expression seems to be replacing all occurrences of 1<br>
with the appropriate  evaluated code.  But, I&#39;m not sure as<br>
&#39;s!($nums[$i++])!s$cols[$j++]$1s!g&#39;  is a bit on the unusual side of things.<br>
<br>
Gautam.<br>
<br>
</div><div><div></div><div class="h5">&gt; @nums = qw(1 1 2);<br>
&gt; @cols = qw(g r y);<br>
&gt; $i=$j=0;<br>
&gt; $s =~ s!($nums[$i++])!s$cols[$j++]$1s!g;<br>
&gt; print &quot;Want: &#39;$t&#39;\n&quot;;<br>
&gt; print &quot;Got:  &#39;$s&#39;\n&quot;;<br>
&gt; print &quot;i is now $i\n&quot;;<br>
&gt; print &quot;j is now $j\n&quot;;<br>
&gt;<br>
&gt; The output of this program is unfortunately:<br>
&gt;<br>
&gt; Want: &#39;sg1s 2 3 sr1s 2 3 1 sy2s 3&#39;<br>
&gt; Got:  &#39;sg1s 2 3 sr1s 2 3 sy1s 2 3&#39;<br>
&gt; i is now 1<br>
&gt; j is now 3<br>
&gt;<br>
&gt; Perl ended up incorrectly coloring the third ‘1′ yellow, instead of the<br>
&gt; third ‘2′, because in the substitution regular expression, the left<br>
&gt; (matching) side was evaluated/compiled only once, while the right<br>
&gt; (replacement) side was evaluated/interpolated three times (as desired).<br>
&gt;<br>
&gt; I know that there are ways to force the left side of the RE to compile only<br>
&gt; once (/o modifier, qr// operator), and there are ways to force eval in the<br>
&gt; right side (/e and /ee modifiers — although in this case, standard<br>
&gt; interpolation was sufficient), but is there any way I can force<br>
&gt; the left side to compile multiple times? (Even within the same /g<br>
&gt; substitution?) I tried a few constructs using the \G (start matching from<br>
&gt; previous position) zero-width assertion, but only managed to get myself into<br>
&gt; infinite loops.<br>
&gt;<br>
&gt; I&#39;d appreciate any help anyone can offer -- including &quot;What an idiot, the<br>
&gt; RIGHT way to do that is completely different! (And here it is...)&quot;<br>
&gt;<br>
&gt; thx for thinking for me!<br>
&gt;<br>
&gt; r<br>
&gt;<br>
&gt;<br>
&gt;<br>
</div></div><div><div></div><div class="h5">&gt; _______________________________________________<br>
&gt; San-Diego-pm mailing list<br>
&gt; <a href="mailto:San-Diego-pm@pm.org">San-Diego-pm@pm.org</a><br>
&gt; <a href="http://mail.pm.org/mailman/listinfo/san-diego-pm" target="_blank">http://mail.pm.org/mailman/listinfo/san-diego-pm</a><br>
&gt;<br>
</div></div></blockquote></div><br></div>