Gossamer Forum
Home : General : Perl Programming :

Reading in $ without tokenizing

Quote Reply
Reading in $ without tokenizing
 I am trying to read in a web page and pull out a dollar amount. The
problem I'm having is when it reads a line with say $45.00 it thinks
the $45 is a variable. I want to read this dollar amount into a $price
variable but all I'm coming up with is .00. How can I read in a line
from a file without it thinking anything following a $ is a variable?

Quote Reply
Re: Reading in $ without tokenizing In reply to
Let's see your code..

--mark
Quote Reply
Re: Reading in $ without tokenizing In reply to
Try puting a backslash before $.
Ie.: \$amount
Quote Reply
Re: Reading in $ without tokenizing In reply to
 Well, kinda hard to post the whole script, and I can't post the $price
since I have nothing that works yet for it. But..

This is what reads in the file. $isbn is a pre-downloaded web page;

@info = stat("newISBN/books/$isbn");
if($info[7] > 10000) {
open(infile,"newISBN/books/$isbn");
$i = 0;
@line = ();
while(<infile> ) {
if($_ !~ /<\/html>|<\/body>/i) { print $_; }
chop $_;
if($_ ne '') { $line[$i++] = $_; }
}
close(infile);
}

The line I am having problems with would be like this;
<b>Our Price: <font color=#990000>$45.00</font></b><br>

Then I'm trying to extract the dollar amount to a $price variable
with somthing like;

$price = $line[$i];
$price =~ s/\<b>Our Price: <font color=#990000>//i;
$price =~ s/\<\/font>\<\/b>\<br>//i;

Now $price is ".00" and not "$45.00". What I really want is to be
able to have $price equal "45.00" so I can then subtrace a percentage
for the discount price. I hope my rambling makes some sort of sence..

ilya: If I were to hand-edit each pre-downloaded file to remove the
$ or put a \ in front of it.. I may as well not write the script. The
idea is I don't want to hand-edit 40,000 files. It would be just as
easy to clip/paste the price.. which is what I am doing now, and what
I want to get away from having to do.
Quote Reply
Re: Reading in $ without tokenizing In reply to
Given the exact code you provided:

Code:
while (<DATA> ) {
/(\$\d+\.?(\d{1,2})?)/;
$price = $1;
}

print "The price is $price\n";


__DATA__
<html>
<head>
<title>This is a test</test>
</head>


<body>
<b>Our Price: <font color=#990000>$45.00</font></b><br>
More text! Yeah!
</body>
</html>

Note: I am using __DATA__ to simulate opening a file and reading from it.

(I keep editing this, as I keep playing with it. The above regex will now pull out any amount, with or without a decimal Smile)

--mark


[This message has been edited by Mark Badolato (edited January 12, 2000).]
Quote Reply
Re: Reading in $ without tokenizing In reply to
 Thanks Mark. I havn't been able to incorperate that yet into
my existing script and have it work.. but I'm not giving up yet.
I'm not well versed in perl, so sort of going with the trial and
error method. So far it seems I'm not holding my mouth right.