Solved

Read one value in file

Posted on 2000-05-03
9
142 Views
Last Modified: 2010-03-05
Hello,

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">
        <tr>
          <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
              (NLG)
            <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>
</small>
          </td>
          <td colspan="2" height="48">&nbsp;</td>
        </tr>
        <tr>
--- 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??
0
Comment
Question by:mmcw
  • 5
  • 2
  • 2
9 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 100 total points
ID: 2773660
try this...

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

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

WITH...

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


=====================guilder.pl
#!/usr/local/bin/perl


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

##  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=$2;
$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!!

$test3=$1;
print $test3;
0
 

Author Comment

by:mmcw
ID: 2776487
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!!
0
 

Author Comment

by:mmcw
ID: 2776491
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>
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:mmcw
ID: 2776593
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:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=10337436 

You told me you had the answer!!
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2777024

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.
0
 

Author Comment

by:mmcw
ID: 2777046
After changing the first line thescript workes!!
0
 
LVL 84

Expert Comment

by:ozo
ID: 2779354
$/=""; #sets paragraph mode, so it will separate records at empty lines.
0
 

Author Comment

by:mmcw
ID: 2783410
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?
0
 
LVL 84

Expert Comment

by:ozo
ID: 2783424
/([\d,]+\.?\d+)/
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Connecting to linux server using Net::Telnet module 25 232
SIMPLE Perl Regex 1 159
pipe to sed or perl, please help 5 128
parse a file and get data out 11 45
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question