Read one value in file


I have a file called test.html
I want to read the file and split it into three part and then split to get the variable $correction.

The following is in the file:

---more html---

      <p> <small> </small>
      <table cellpadding=0 cellspacing=0 height="99">
          <td nowrap valign=top height="48" width="300"> <br>
            Wednesday, May 3, 2000 <!-- conversion result starts  -->
            <p><b><font color="#153168"> <font color="#bc0a17">1</font>
              Dutch Guilder = <font color="#bc0a17"> 0.4125</font>
              US Dollar </font></b> <!-- conversion result ends  --> <br>
              1 US Dollar (USD) = 2.4242 Dutch Guilder
            <p> <small>Median price was 0.4122 / 0.4125 (bid/ask).<BR>
    Minimum price was 0.4098 / 0.4099 <BR>
    Maximum price was 0.4163 / 0.4168 <BR>
          <td colspan="2" height="48">&nbsp;</td>
--- more html------

I now want to split the file into three parts. Above <!-- conversion result starts  -->

Between <!-- conversion result starts  -->  and <!-- conversion result ends  -->

and down <!-- conversion result ends  -->

Now I want to split the middle part:

Here I have three variables:
$test1 = "Dutch Guilder";
$test2 = "US Dollar";

I want to split this part untill I have the value (0.4125) before $test2.
The value has to be called $test3.

Can someone help me??
Who is Participating?
maneshrConnect With a Mentor Commented:
try this...

if the HTML tags in you file are more complicated, then use can use the foll piece of code.

$html_text =~ s/<[^>]*>//g; ##  Remove any HTML tags


use HTML::Parse;
use HTML::FormatText;
$html_text = HTML::FormatText->new->format(parse_html($html_text));

##  Read the ENTIRE html file in a variable.
open(HTM,"/tmp/more.html") || die $!;

##  Remove the \n chars from that variable.
$file=~ s/\n//g;
##  Extract the relevent part.
$file=~ /(.*)<\!-- conversion result starts  -->(.*)\!-- conversion result ends

##  Store the extracted part in a variable.
$html_text =~ s/<[^>]*>//g; ##  Remove any HTML tags
$html_text=~ s/\s+/ /g;     ##  Squeeze multiple white spaces to a single space
$html_text=~ /$Dutch Guilder\s+=\s+(\d+\.?\d+)/;  ##  Get the currency value!!

print $test3;
mmcwAuthor Commented:
The part of extracting does not work!!

##  Extract the relevent part.
$file=~ /(.*)<\!-- conversion result starts  -->(.*)\!-- conversion result ends

The result of this is the same as when not extracting!!

Can you help me!!
mmcwAuthor Commented:
By the way: I had to change the starting code from: $/="";
to: undef $/; to get the whole html file!!

Otherwise you get only the part till </head>
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

mmcwAuthor Commented:
The extrating works when you change the starting code. See my last comment.

Thank you.

Could you take a look to the question I asked: 

You told me you had the answer!!

glad to know that undef $/; worked for you.

i have a Q for you though.

what version of PERL are you using?. What platform are you running the script on?? what webserver areyou running???

the reason i want to know this is because $/=""; works fine on my solaris box.

"Could you take a look to the question I asked: "

pl. check my comments.
mmcwAuthor Commented:
After changing the first line thescript workes!!
$/=""; #sets paragraph mode, so it will separate records at empty lines.
mmcwAuthor Commented:
Question: Sometimes the number looks like this: 0.4125

The script workes fine.

But somethimes the number looks like this: 1,926.22

The script will not work.

I have checked the script:

It will work till:
$html_text =~ /$$base_currency\s+=\s+(\d+\.?\d+)/;

The the sesult will be "";

Can zou fix it to make it work for both format of numbers?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.