<br><br><div class="gmail_quote">On Wed, Feb 23, 2011 at 1:51 AM, John Ricker <span dir="ltr">&lt;<a href="mailto:sephtin%2Bpm-talk@gmail.com">sephtin+pm-talk@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;">
Very nice... <div>I assumed that it&#39;d have to be done via some sort of loop structure.. never crossed my mind that I could shift through the array to accomplish this.  :)</div><div><br></div><div>Sitting in front of the code, I agree that it would work.  The marker does not change, the replacements are already in an array, and the file could easily be grabbed in a string.</div>

<div>Thanks again for the assistance!</div><div><br></div><div>This is too easy.  :(</div></blockquote><div><br></div><div>s/too easy/perl/</div><div><br></div><div>:)</div><div><br></div><div>mike </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div>I&#39;ll see if I can find something more fun and difficult.. like maybe instead of having to call system (&quot;xxd...&quot;) to convert the binary to hex, see if I can find a way to do it in Perl as well.. if so, then I think I&#39;ve eliminated the last &quot;system&quot; call in my script that can possibly be eliminated... ;)</div>

<div><br></div><div>Anyway, thanks again!</div><div>-John</div><div><div></div><div class="h5"><div><br><div class="gmail_quote">On Wed, Feb 23, 2011 at 2:05 AM, Mike South <span dir="ltr">&lt;<a href="mailto:msouth@gmail.com" target="_blank">msouth@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><div><font face="arial, helvetica, sans-serif">If you have the whole file in a string and the replacements in an array and the &#39;marker&#39; doesn&#39;t change, I think the following will work.</font></div>


<div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">The /e modifier executes code, so s/foo/ do something here /eg will replace each instance of foo with whatever &#39;do something here&#39;, executed as perl code, returns.  So I just made it grab the next color off the list and put that in place.</font></div>


<div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">In case you have more of the &#39;markers&#39; than you have replacements, I just put $1 back in right where we found it.  The stuff that&#39;s printing out the length of the string is a rough check to make sure I&#39;m not accidentally clobbering part of the string. </font></div>


<div><font face="arial, helvetica, sans-serif"><br></font></div><div><font face="arial, helvetica, sans-serif">mike</font></div><div style="font-family:&#39;courier new&#39;, monospace">
<br></div><div style="font-family:&#39;courier new&#39;, monospace"><br></div><div><div><font face="&#39;courier new&#39;, monospace">use strict;</font></div><div><font face="&#39;courier new&#39;, monospace">use warnings;</font></div>


<div><font face="&#39;courier new&#39;, monospace"><br></font></div><div><font face="&#39;courier new&#39;, monospace">my $file = &#39;</font></div><div><font face="&#39;courier new&#39;, monospace">08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 09 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 00 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 00 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 00 FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF 08 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 08 00 00 1C FF FF FF FF 08 00 00 01 05 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 00 05 00 08 00 00 1C 00 00 FF FF FF FF 08 00 00 05 01 0F 00 08 00 00 1C 00 18 00 00 &#39;;</font></div>


<div><font face="&#39;courier new&#39;, monospace"><br></font></div><div><font face="&#39;courier new&#39;, monospace">my $length = length $file;</font></div><div><font face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font face="&#39;courier new&#39;, monospace">my $color1 = &#39;11#AA#BB#CC&#39;;</font></div><div><font face="&#39;courier new&#39;, monospace">my $color2 = &#39;22#DD#EE#FF&#39;;</font></div>
<div><font face="&#39;courier new&#39;, monospace">my $color3 = &#39;33#XX#XX#XX&#39;;</font></div><div><font face="&#39;courier new&#39;, monospace">my @replacements = ($color1, $color2, $color3);</font></div>
<div><font face="&#39;courier new&#39;, monospace">#my @replacements = ($color1, $color2);</font></div><div><font face="&#39;courier new&#39;, monospace"><br></font></div>
<div><font face="&#39;courier new&#39;, monospace">my $marker = &#39;08 00 00 1C &#39;;</font></div><div><font face="&#39;courier new&#39;, monospace">my $xx = qr/[[:xdigit:]]{2}/; # two hex digits</font></div>
<div><font face="&#39;courier new&#39;, monospace">$file =~ s/$marker(($xx ){3}$xx)/$marker . (shift(@replacements) || $1)/ge;</font></div><div><font face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font face="&#39;courier new&#39;, monospace"><br></font></div><div><font face="&#39;courier new&#39;, monospace">print $file,$/;</font></div><div><font face="&#39;courier new&#39;, monospace">print &quot;length was $length, now it&#39;s &quot;, length($file), $/;</font></div>


</div><div style="font-family:&#39;courier new&#39;, monospace"><br></div></div><div><div></div><div><br><div class="gmail_quote">On Tue, Feb 22, 2011 at 11:49 PM, Mike South <span dir="ltr">&lt;<a href="mailto:msouth@gmail.com" target="_blank">msouth@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">Also those \d&#39;s aren&#39;t going to match A-F are they?<div><br></div><div>Are you familiar with qr// to let you store a regex bit in a variable?  That might make some of this more readable.</div>


<div><br></div><font color="#888888"><div>mike</div></font><div><div></div><div>
<div><br><div class="gmail_quote">On Tue, Feb 22, 2011 at 10:58 PM, Matt Nash <span dir="ltr">&lt;<a href="mailto:mattnashbrowns@gmail.com" target="_blank">mattnashbrowns@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">



Of course, you want actual spaces (not &#39;\s&#39;) in the replace string, but you knew that.<div><div></div><div><br><br><div class="gmail_quote">On Tue, Feb 22, 2011 at 9:56 PM, Matt Nash <span dir="ltr">&lt;<a href="mailto:mattnashbrowns@gmail.com" target="_blank">mattnashbrowns@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>Hi John,</div><div><br></div><div>You are reading the entire file into an array of lines, then processing that array using a C-style for loop and interpolating the index var into the search string... and that is the LEAST crazy thing. :)  It works, of course, because it turns out that there are infinitely many ways to do it.<br>





<br></div><div>For your immediate problem of the search-and-replace, I recommend reading the  whole file into a single string, newlines and all, then using the /gc modifiers in the regex.  Maybe put your colorChange stuff into an array that you loop through, checking the regex for the nth match:</div>





<div><br></div><div>@changes = ($change1, $change2, $change3);</div><div><br></div><div>foreach $change (@changes) {</div><div>    $wholefile =~ s|08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d|08\s00\s00\s1C\s$change|gc;</div><div>





}</div><div><br></div><div>see <a href="http://perldoc.perl.org/perlre.html" target="_blank">http://perldoc.perl.org/perlre.html</a> and <a href="http://perldoc.perl.org/perlretut.html#Using-regular-expressions-in-Perl" target="_blank">http://perldoc.perl.org/perlretut.htm</a> for lots of details, but the upshot of the modifiers is: g makes it keep looking for matches; c tells it to remember where it last matched, and start from there on the next match.  You don&#39;t have to care what n is, because you are matching your search string exactly as many times as you have changes to make.</div>





<div><br></div><div>...but this project sounds ripe for refactoring, if I may be so bold.  Could it be that what you really need is a templating system?</div><div><br></div><div>Thanks for bringing some much-needed questions to this list!</div>





<div><br></div><div>Matt</div><div><br><div class="gmail_quote"><div><div></div><div>On Tue, Feb 22, 2011 at 8:31 PM, John Ricker <span dir="ltr">&lt;<a href="mailto:sephtin%2Bpm-talk@gmail.com" target="_blank">sephtin+pm-talk@gmail.com</a>&gt;</span> wrote:<br>





</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div><div>Received so much help with the last question (which seems to be functioning great, btw.. thanks all)... thought I&#39;d try again.  ;)<div>





<br></div><div>Have a script that was recently migrated to Perl from shell/bash, and am wondering if there&#39;s an easy way in Perl to do the following-</div>
<div><br></div><div>File(s) being modified are hex, and look something like:</div><div>---x---</div><div>...</div><div>08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 09 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 00 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 00 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 00 FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF 08 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 FF FF FF FF 08 00 00 01 05 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 00 05 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 </div>






<div>...</div><div>---x---</div><div>File(s) contain several occurrences of &quot;08 00 00 1c ## ## ## ##&quot; or more appropriately for this list: &quot;08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d&quot;</div><div><br></div>





<div>
Now on to the good stuff...  I would like to replace the ## ## ## ## with my chosen colors, that are being provided by vars... Example:</div><div>colorChange1=&quot;FF 00 00 FF&quot;</div><div>colorChange2=&quot;FF FF 00 FF&quot;</div>






<div>colorChange3=&quot;FF 00 FF FF&quot;</div><div>...</div><div><br></div><div>I&#39;m wondering how it might be possible, to replace the FIRST occurrence (in the file, NOT in a line) of &quot;08 00 00 1C ## ## ## ##&quot; with &quot;08 00 00 1C $colorChange1&quot;, the second with &quot;08 00 00 1C $colorChange2&quot;, etc.</div>






<div><br></div><div>More info:</div><div>--Script modifies files to theme them, in this case, I&#39;m taking a BINARY (compiled XML) file, converting it to HEX via xxd, and then changing the text color for a theme via substitution.</div>






<div>--I originally thought it could be done similar to what sed does... with s|(08\s00\s00\s1C)\s\d\d\s\d\d\s\d\d\s\d\d|$1$colorChange1|1  (note last digit...), but testing has shown that this doesn&#39;t work as expected.</div>






<div>--Not really related, but in the binary, the colors are actually backwards, so color - FF AB CD EF becomes binary - EF CD AB FF, but that&#39;s an easy change.</div><div>--CURRENTLY in my script, I&#39;m doing this via a sub, passing an array of the colors, the file, and the file location (directory), and pulling the file, making the changes, and saving it back out via loop... BUT, because I couldn&#39;t figure out how to just do the multiple replaces... I cheated and made template files that contain &quot;08 00 00 1C 11 11 11 11&quot; and &quot;08 00 00 1C 22 22 22 22&quot;, so when I iterate through the loop, I just change like so:</div>






<div>for ( $i = 1 ; $i &lt;= $COUNT ; $i++ ) {</div><div>...</div><div>    $line =~ s|08\s00\s00\s1c\s$i$i\s$i$i\s$i$i\s$i$i\s|08 00 00 1c $array_ref[$i]|;</div><div>...</div><div>}</div><div><br></div><div>Code works, but I&#39;d much rather be able to pull native files straight out of the .zip, and change them that way... :P</div>






<div><br></div><div>Anyway, again, if there are specifics I missed, happy to provide them.  </div><div>I keep thinking there should be an easy way to do this... but my google-fu is failing me.. </div><div><br></div><div>





Thanks again for the help with the previous problem, and in advance for any assistance on this one.  :)</div>
<div>At the very least, I guess I can provide some chatter to the group.</div><div><br></div><div>-John (sephtin @gmail)</div>
<br></div></div>_______________________________________________<br>
Raleigh-talk mailing list<br>
<a href="mailto:Raleigh-talk@pm.org" target="_blank">Raleigh-talk@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/raleigh-talk" target="_blank">http://mail.pm.org/mailman/listinfo/raleigh-talk</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br>
</div></div><br>_______________________________________________<br>
Raleigh-talk mailing list<br>
<a href="mailto:Raleigh-talk@pm.org" target="_blank">Raleigh-talk@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/raleigh-talk" target="_blank">http://mail.pm.org/mailman/listinfo/raleigh-talk</a><br>
<br></blockquote></div><br></div>
</div></div></blockquote></div><br>
</div></div><br>_______________________________________________<br>
Raleigh-talk mailing list<br>
<a href="mailto:Raleigh-talk@pm.org" target="_blank">Raleigh-talk@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/raleigh-talk" target="_blank">http://mail.pm.org/mailman/listinfo/raleigh-talk</a><br>
<br></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Raleigh-talk mailing list<br>
<a href="mailto:Raleigh-talk@pm.org">Raleigh-talk@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/raleigh-talk" target="_blank">http://mail.pm.org/mailman/listinfo/raleigh-talk</a><br>
<br></blockquote></div><br>