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="'courier new', monospace">DIST - Distance</font></div><div><font face="'courier new', monospace">   DISTANCE:  12.3456 ft   4.5678 m</font></div><div><font face="'courier new', monospace">   STD DEV:   3.4567 ft   1.2345 m</font></div>
<div><font face="'courier new', monospace"><br></font></div><div><font face="'courier new', monospace">AREA - Area</font></div><div><font face="'courier new', monospace">Â Â Â AREA: Â Â Â 2345.6789 ft^2 Â 345.6789 m^2</font></div>
<div><font face="'courier new', monospace">   PERIMETER:  234.5678 ft    89.0123 m</font></div><div><font face="'courier new', monospace">   STD DEV:    3.4567 ft    1.2345 m</font></div><div><font face="'courier new', monospace"><br>
</font></div><div><font face="'courier new', monospace">DIST - Distance</font></div><div><font face="'courier new', monospace">   DISTANCE:  12.3456 ft  4.5678 m</font></div><div><font face="'courier new', monospace">   STD DEV:   3.4567 ft   1.2345 m</font></div>
<div><font face="'courier new', monospace"><br>DIST - Distance</font></div><div><font face="'courier new', monospace">   DISTANCE:   8.9012 ft   3.0123 m</font></div><div><font face="'courier new', monospace">   STD DEV:   1.2345 ft   0.4567 m</font></div>
<div><font face="'courier new', 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/'STD DEV'/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="'courier new', monospace">for $i (0..$#nums) {</font></div><div><font face="'courier new', monospace">Â Â $str =~ s{($function.*$metric.*)(?<!>)($vals[$i]\s+$unit)}</font></div><div>
<font face="'courier new', monospace">Â Â Â Â Â Â Â {$1<span color="$cols[$i]">$2</span>}</font></div>
<div><font face="'courier new', monospace">}</font></div><div>didn'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'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="'courier new', monospace">$head = '';</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">$tail = $s;</font></div><div><font class="Apple-style-span" face="'courier new', monospace">for my $i ( 0 .. $#nums ) {</font></div><div>
<font class="Apple-style-span" face="'courier new', monospace">Â Â Â $tail =~ s/(.*?$function.*?$metric.*?)($nums[$i]\s+$unit)(.*)/$3/;</font></div><div><font class="Apple-style-span" face="'courier new', monospace">Â Â Â $head .= "$1<span color="$cols[$i]">$2</span>";</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">}</font></div><div><font class="Apple-style-span" face="'courier new', monospace">$s = "$head$tail";</font></div><div><br></div>
<div>If there isn'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"><<a href="mailto:gautam.dey77@gmail.com">gautam.dey77@gmail.com</a>></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 <<a href="mailto:ruberad@gmail.com">ruberad@gmail.com</a>>:<br>
<snip><br>
<div class="im">><br>
> #! /bin/perl<br>
> $s = "1 2 3 1 2 3 1 2 3"; Â Â Â Â Â # what I have<br>
> $t = "sg1s 2 3 sr1s 2 3 1 sy2s 3"; # 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 "sg1s 2 3" and that there are three elements after the 1 to generate the<br>
"sr1s 2 3 1" and that there is only 1 element after the 2 to generate the<br>
"sy2s 3". 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'm not sure as<br>
's!($nums[$i++])!s$cols[$j++]$1s!g' Â is a bit on the unusual side of things.<br>
<br>
Gautam.<br>
<br>
</div><div><div></div><div class="h5">> @nums = qw(1 1 2);<br>
> @cols = qw(g r y);<br>
> $i=$j=0;<br>
> $s =~ s!($nums[$i++])!s$cols[$j++]$1s!g;<br>
> print "Want: '$t'\n";<br>
> print "Got: Â '$s'\n";<br>
> print "i is now $i\n";<br>
> print "j is now $j\n";<br>
><br>
> The output of this program is unfortunately:<br>
><br>
> Want: 'sg1s 2 3 sr1s 2 3 1 sy2s 3'<br>
> Got: Â 'sg1s 2 3 sr1s 2 3 sy1s 2 3'<br>
> i is now 1<br>
> j is now 3<br>
><br>
> Perl ended up incorrectly coloring the third ‘1′ yellow, instead of the<br>
> third ‘2′, because in the substitution regular expression, the left<br>
> (matching) side was evaluated/compiled only once, while the right<br>
> (replacement) side was evaluated/interpolated three times (as desired).<br>
><br>
> I know that there are ways to force the left side of the RE to compile only<br>
> once (/o modifier, qr// operator), and there are ways to force eval in the<br>
> right side (/e and /ee modifiers — although in this case, standard<br>
> interpolation was sufficient), but is there any way I can force<br>
> the left side to compile multiple times? (Even within the same /g<br>
> substitution?) I tried a few constructs using the \G (start matching from<br>
> previous position) zero-width assertion, but only managed to get myself into<br>
> infinite loops.<br>
><br>
> I'd appreciate any help anyone can offer -- including "What an idiot, the<br>
> RIGHT way to do that is completely different! (And here it is...)"<br>
><br>
> thx for thinking for me!<br>
><br>
> r<br>
><br>
><br>
><br>
</div></div><div><div></div><div class="h5">> _______________________________________________<br>
> San-Diego-pm mailing list<br>
> <a href="mailto:San-Diego-pm@pm.org">San-Diego-pm@pm.org</a><br>
> <a href="http://mail.pm.org/mailman/listinfo/san-diego-pm" target="_blank">http://mail.pm.org/mailman/listinfo/san-diego-pm</a><br>
><br>
</div></div></blockquote></div><br></div>