Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 26 Apr 2004 09:50:31 -0700
From:      Drew Tomlinson <drew@mykitchentable.net>
To:        FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Perl Help For Newbie
Message-ID:  <408D3DD7.1050607@mykitchentable.net>

next in thread | raw e-mail | index | archive | help
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">&quot;A&quot; 
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?408D3DD7.1050607>