monkeriffic has asked for the wisdom of the Perl Monks concerning the following question:
But I am just beginning Perl, and things I think are easy turn out not to me. Now, (said in Scarface voice) Let me introduce you to my lil' friend!
My task is sooo deceptively simple: Just replace a simple string with another string. How hard could that be?
My data file is here: http://home.comcast.net/~tankomail/preg.htm And a sample is at the very bottom of this post. I just want to replace /<form[.*]?*\/form>/with the word "block"
Basically I just want to replace all <form> </form> fields and everything in between with nothing, but in testing, I wanted to see my work so I chose the word "block" as a good simple substitute which I could then replace with nothing.
Way Below is my base code. But here, just under is the pulled line from the base code that seems to be the issue:
$orgtext = Whey; # this one right here $newtext = Popcorn;
The above works. I reduced it to it's simplest form as a sanity check. Then I tried:
$orgtext = /[Ww]hey/; # this one right here $newtext = Popcorn;
But beyond the most primitive replacement, I invariably get:
Use of uninitialized value in pattern match (m//) at C:\russ\scripts\_Master_Snippets\clean_2_input_output_file.pl line 9.
Eventually I want to try:
$orgtext = /<form[.*]?*\/form>/; # this one right here $newtext = block;
But I can't get past the staring blocks. I know this code works in general, but my modifications seem to break it.
I also tried some while (<$intext>) variations, even removing the undef $/ slurp line, so that the intext would receive the data line by line - but no luck anywhere. I have spent quite a bit of research time trying various things - but apparently it's not a trivial task.
Any suggestions as to:
1.) Is my basic model okay, slurping the whole file into a variable? or 2.) Should I use a while <> structure?
And even when I do get the simple Whey replaced with Popcorn - it only does the first instance, basically, I am guessing, because there is no iterative code in this script. And the only iterative examples I've seen are not with a whole file in one "intext" variable, but always with a while <> structure.
Your input and examples are GREATLY appreciated because the red spot on my banging against the cubicle wall head is growing.
L, Sam
---------------------------
Here is my base code.
$infile = 'C:\russ\weights\preg.htm'; $outfile = 'C:\russ\weights\preg_clean.htm'; # No, I am not pregnant, but I am helping a pregnant woman out! No...n +ot just helping her get # her start either :) $orgtext = Whey; $newtext = Popcorn; undef $/; #slurp mode, read files in a whole open IN, $infile or die $!; $intext = <IN>; close IN; $intext =~ s/$orgtext/$newtext/ms; # the ms is for coping correctly with newlines (that can easily appear + in a binary). open OUT, ">$outfile" or die $!; print OUT $intext; close OUT; # replaces ALL occurrences of orgtext with newtext and places the numb +er of occurences in $count
--------data sample. link to complete data above
<table width="100%" border="0" align="center" cellpadding="0" cellspac +ing="0"> <tr> <td width="24%" rowspan="2" valign="top"><table width="198" border +="0" align="center" cellpadding="1" cellspacing="0"> <tr> <td><div align="center"><img src="images/ls_logo.gif" width= +"192" height="91"></div></td> </tr> <tr> <td valign="top"><table width="200" border="0" align="center +" cellpadding="2" cellspacing="3"> <tr> <td width="197" valign="top"><table width="100%" borde +r="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><div align="center"><a href="all-products.ht +ml"><img src="2005-menu/all-prods.gif" name="all" width="177" height= +"33" border="0"></a></div></td> </tr> <tr> <td><div align="center"><a href="vitamins-supple +ments.html"><img src="2005-menu/vits-supl.gif" name="vitamins" width= +"177" height="33" border="0"></a></div></td> </tr> <tr> <td><div align="center"><a href="liquid-suppleme +nts.html"><img src="2005-menu/liquid-vit.gif" name="liquid" width="17 +7" height="33" border="0"></a></div></td> </tr> <tr> <td><div align="center"><a href="body-building.h +tml"><img src="2005-menu/body-build.gif" name="bodybuild" width="177" + height="33" border="0"></a></div></td> </tr> <tr> <td><div align="center"><a href="weightloss.html +"><img src="2005-menu/diet.gif" name="diet" width="177" height="33" b +order="0"></a></div></td> </tr> <tr> <td><div align="center"><a href="body-essentials +.html"><img src="2005-menu/body-ess.gif" name="bodyess" width="177" h +eight="33" border="0"></a></div></td> </tr> <tr> <td><div align="center"><a href="articles.html"> +<img src="2005-menu/articles.gif" alt="Articles of Interest" name="ar +ticles" width="177" height="33" border="0"></a></div></td> </tr> </table></td> </tr> <tr> <td><div align="center"> </div></td> </tr> <tr> <td><div align="center"> <form method=POST style="marg +in-bottom: 0" action="https://www.linkpointcart.net/cgi-bin/cart.cgi" +> <input type=hidden name="ViewCart" value=" +ThreadsCart"> <input type=submit value="View Cart"> </form></div></td> </tr> <tr> <td><div align="center"><form method=POST style="margi +n-bottom: 0" action="https://www.linkpointcart.net/cgi-bin/cart.cgi"> <input type=hidden name="CheckOut" value="Online"> <input type=hidden name="CartID" value="ThreadsCart"> <input type=submit value="Check Out"> </form></div></td> </tr> <tr> <td><table width="100%" border="0" cellspacing="0" cel +lpadding="0"> <tr> <td><br><div align="center"><a href="catalog.htm +l"><img src="2005-menu/catalog-banner.gif" width="196" height="50" bo +rder="0"></a></div></td> </tr> </table> <div align="center"><font size="2" face="Arial, Helv +etica, sans-serif"><strong><br> We want to hear from you.<br> Suggest a NEW PRODUCT!!<br> <a href="suggest.htm">:: click here::</a></strong> +</font></div></td> </tr> </table></td> </tr> </table></td> <td width="76%" height="28" valign="top"><div align="right"><img s +rc="2005-menu/top-image.gif" width="604" height="98" border="0" usema +p="#Map"></div></td> </tr> <tr> <td valign="top"><br> <!-- InstanceBeginEditable name="content" -- +> <table width="90%" border="0" align="center" cellpadding="1" cel +lspacing="1"> <tr> <td><table width="560" border="0" align="center" cellpadding +="3" cellspacing="0"> <tr> <td rowspan="2" valign="top"><div align="center"><img +src="bottles/whey-chocolate-s.gif" width="102" height="150" border="0 +"><br> <font color="#666666" size="1" face="Arial, Helvet +ica, sans-serif"></font></div></td> <td><div align="left"><font size="2" face="Arial, Helv +etica, sans-serif"><strong><font size="3">Whey Protein<br> Chocolate 3.3 lbs.</font><br> 54 grams of protein per serving<br> <br> </strong></font><font face="Verdana, Arial, Helvet +ica, sans-serif"><strong><font size="3" face="Arial, Helvetica, sans- +serif">$ 39.99</font></strong></font></div></td> <td rowspan="2" valign="top"><div align="center"><img +src="bottles/whey-vanilla-s.gif" width="102" height="150" border="0"> +<br> </div></td> <td><div align="left"><font size="2" face="Arial, Helv +etica, sans-serif"><strong><font size="3">Whey Protein<br> Vanilla </font><font size="2" face="Arial, Helveti +ca, sans-serif"><strong><font size="3">3.3 lbs.</font></strong></font><br> 54 grams of protein per serving.<br> <br> </strong></font><font face="Verdana, Arial, Helvet +ica, sans-serif"><strong><font size="3" face="Arial, Helvetica, sans- +serif">$ 39.99</font></strong></font><font size="2" face="A +rial, Helvetica, sans-serif"><strong> </strong></font></div></td> </tr> <tr> <td><form method="post" action="https://www.linkpointc +art.net/cgi-bin/cart.cgi"> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td><font size="2" face="Arial, Helvetica, san +s-serif">Quantity:</font></td> <td><font face="Verdana, Arial, Helvetica, san +s-serif"> <input type="text" name="VARQuantity" value= +"1" size="4" /> </font></td> </tr> <tr> <td colspan="2" align="center"> <font face="Ve +rdana, Arial, Helvetica, sans-serif"> <input type="hidden" name="VAR000" value="|" + /> <input type="hidden" name="AddItem" value="T +hreadsCart|Lifesource Labs - Whey Protein Powder Chocolate VAR000 $39 +.99|VARQuantity|||price5|||||||" /> <input name="submit" type="submit" value="Ad +d To Cart" /> </font></td> </tr> </table> </form></td> <td><form method="post" action="https://www.linkpointc +art.net/cgi-bin/cart.cgi"> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td><font size="2" face="Arial, Helvetica, san +s-serif">Quantity:</font></td> <td><font face="Verdana, Arial, Helvetica, san +s-serif"> <input type="text" name="VARQuantity2" value +="1" size="4" /> </font></td> </tr> <tr> <td colspan="2" align="center"> <font face="Ve +rdana, Arial, Helvetica, sans-serif"> <input type="hidden" name="VAR000" value="|" + /> <input type="hidden" name="AddItem" value="T +hreadsCart|Lifesource Labs - Whey Protein Powder Vanilla VAR000 $39.9 +9|VARQuantity|||price5|||||||" /> <input name="submit" type="submit" value="Ad +d To Cart" /> </font></td> </tr> </table> </form></td>
2006-10-14 Retitled by GrandFather, as per Monastery guidelines
Original title: 'I have no problems eating cereal...after it softens. Why is replacing a simple string so hard then?'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to use a regex to parse html form tags
by GrandFather (Saint) on Oct 14, 2006 at 01:17 UTC | |
|
Re: How to use a regex to parse html form tags
by Zaxo (Archbishop) on Oct 14, 2006 at 01:19 UTC | |
|
Re: How to use a regex to parse html form tags
by ammon (Sexton) on Oct 14, 2006 at 01:29 UTC | |
|
Re: How to use a regex to parse html form tags
by bart (Canon) on Oct 14, 2006 at 07:19 UTC | |
|
Re: How to use a regex to parse html form tags
by graff (Chancellor) on Oct 14, 2006 at 07:20 UTC | |
|
Re: How to use a regex to parse html form tags
by fenLisesi (Priest) on Oct 14, 2006 at 10:37 UTC | |
|
Re: How to use a regex to parse html form tags
by blazar (Canon) on Oct 14, 2006 at 10:25 UTC |