From owner-freebsd-questions@FreeBSD.ORG Mon Apr 26 09:50:29 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 52D2116A4EB for ; Mon, 26 Apr 2004 09:50:29 -0700 (PDT) Received: from relay02.roc.ny.frontiernet.net (relay02.roc.ny.frontiernet.net [66.133.131.35]) by mx1.FreeBSD.org (Postfix) with ESMTP id 6197443D31 for ; Mon, 26 Apr 2004 09:50:28 -0700 (PDT) (envelope-from drew@mykitchentable.net) Received: (qmail 22766 invoked from network); 26 Apr 2004 16:50:27 -0000 Received: from 67-51-156-100.dsl1.elk.ca.frontiernet.net (HELO blacklamb.mykitchentable.net) ([67.51.156.100]) (envelope-sender )SMTP for ; 26 Apr 2004 16:50:27 -0000 Received: from mykitchentable.net (unknown [165.107.42.233]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by blacklamb.mykitchentable.net (Postfix) with ESMTP id CD1843BF396 for ; Mon, 26 Apr 2004 09:50:25 -0700 (PDT) Message-ID: <408D3DD7.1050607@mykitchentable.net> Date: Mon, 26 Apr 2004 09:50:31 -0700 From: Drew Tomlinson User-Agent: Mozilla Thunderbird 0.5 (Windows/20040207) X-Accept-Language: en-us, en MIME-Version: 1.0 To: FreeBSD Questions Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Perl Help For Newbie X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 26 Apr 2004 16:50:29 -0000 I'm trying to write a perl script to modify a web page. The source page is full of lines such as: A Magician Among the Spirits - Houdini $75.00 $67.50 $125.00
"A" Material - Jim Pace $18.00 $16.20 $29.95
Absolute Magic - Derren Brown $24.00 $22.80 $39.95
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 Magician Among the Spirits - Houdini $112.50 $125.00
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 = (); 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 Magician Among the Spirits - Houdini $112.5 $67.50 $125.00
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