Perl Help For Newbie
Drew Tomlinson
drew at mykitchentable.net
Mon Apr 26 09:50:29 PDT 2004
I'm trying to write a perl script to modify a web page. The source page
is full of lines
such as:
<a href="../../catalog/books/html/amagicianamongthespirits.html">A
Magician Among the Spirits - Houdini </a>$75.00 $67.50 $125.00<br>
<a href="../../catalog/books/html/amaterial.html">"A"
Material - Jim Pace</a> $18.00 $16.20 $29.95<br>
<a href="../../catalog/books/html/absolutemagic.html">Absolute Magic -
Derren Brown</a> $24.00 $22.80 $39.95<br>
I want to take the first amount and multiply it by 1.5 and replace it,
remove the second amount, and keep the third
amount the same. So for example, the first line would be converted to:
<a href="../../catalog/books/html/amagicianamongthespirits.html">A
Magician Among the Spirits - Houdini </a>$112.50 $125.00<br>
I am brand new to Perl but have been reading and experimenting for the
past two weeks.
I've managed to open my file and read the contents into an array called
"@page":
open(DATA, "< $input") or die "Couldn't read from datafile: $!\n";
my @page = (<DATA>);
Now I am trying to use the s/// operator to perform the math and
substitution. I get
close to what I want but I'm not quite there. This code
foreach (@page) {
$_=~ s/^\s+//gm; #removes leading whitespace
$_=~ s/\d+\.\d\d/$&*1.5/e; #finds 1st $ amount and adds 50%
}
produces this output:
<a href="../../catalog/books/html/amagicianamongthespirits.html">A
Magician Among the Spirits - Houdini </a>$112.5 $67.50 $125.00<br>
How can I format the converted amount back to US dollars ($112.50)?
I've seen
subroutines to format US currency but can those be used with my current
approach?
Would "printf" be a possible choice? Should I use the "split" function
to separate the
data in fields such as link, description, price1, price2, price3 and
then rebuild each
line with concatenation? Is there some other way?
Any guidance as to the best way to approach this task would be most
appreciated. I've
done lots of reading but haven't found anything that teaches me how to
"think" about
building this script.
Thanks,
Drew
More information about the freebsd-questions
mailing list