Perl Help For Newbie
Aaron Peterson
aaron at alpete.com
Mon Apr 26 10:57:53 PDT 2004
> On 4/26/2004 10:07 AM Aaron Peterson wrote:
>
>>>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.
>>>
>>>
>>
>>probably the best way to approach this is writing a script to generate
>> the
>>complete html in multiple formats instead of writing a script to search
>>through html to find values, calculate, and replace. what i mean, is if
>>you had a single file with the US dollar values for everything, then you
>>wrote a script that used those values to generate complete html pages
>>(doing whatever conversions you needed in the process), that would
>>probably be easier than searching through pre-existing html and doing
>>substitution via regex. then in the future you would only have to change
>>prices in one place and re-run the script, or change the conversion
>>algorithm and rerun the script to get all new html pages. (html, xml and
>>other markup is notoriosly difficult to regex)
>>
>>
> This makes sense but how would I keep the files "in sync". I mean how
> would I be sure that $xx.xx amount corresponded to y product? Would it
> just "work" because each entry in the description array would have a
> corresponding entry in the price array? My fear is getting off by one
> and then having every entry after that be incorrect. Is this a big risk?
>
> Thanks for your reply. I know I have a lot to learn.
I'm saying with a text data file like the following (data.txt):
Item Number One:3.50
Item Number Two:2.25
Item Number Three:300
Item Number Four:25.75
You might write a script something like this (example.pl) to generate HTML:
#!/usr/bin/perl
open(DATA, "<", "data.txt") or die "Couldn't open data file\n";
@data = <DATA>;
close(DATA);
open(HTML, ">", "output.html");
print HTML qq|
<html>
<head>
<title>Example HTML Output</title>
</head>
<body>
<table>
|;
foreach (@data) {
chomp(($description,$dollars) = split /:/);
$converted_value = $dollars * 1.13;
print HTML qq|<tr>
<td>$description</td>
<td>\$$dollars</td>
<td>\$$converted_value</td>
</tr>
|;
}
print HTML "</table></body></html>";
close(HTML);
Hope that helps give you ideas.
aaron
More information about the freebsd-questions
mailing list