DBAsupport.com Forums - Powered by vBulletin
Results 1 to 3 of 3

Thread: Need help with Perl Script

  1. #1
    Join Date
    Mar 2001
    Posts
    2

    Question

    Can anyone help me rewrite the following part of perl script. I need to add another url if I want to count for Canada.

    &get_url;

    $number_of_tries = 0;
    while ($found eq "FALSE" && $number_of_tries < 50) {
    &get_url; # set $found in this function.
    sleep 10; # sleep 10 seconds before trying again.
    $number_of_tries += 1;
    }

    }
    #
    #
    #
    sub set_url {
    $url = "http://www.ny.frb.org/pihome/statistics/forex12.shtml
    }
    #
    #

    Please let me know whether you need to see the whole script.
    You can send me e-mail at katebagin@yahoo.com

    Thank you.

  2. #2
    Join Date
    Jun 2000
    Posts
    417
    What do you need it to do? You said you need to add another URL, but I'm not quite sure what you mean by that.

    Do you just mean that multiple calls to set_url should return different URL's? You could just rewrite the function as


    #global variables
    @urls = ( "url1", "url2", "url3" );
    $url_index = 0;

    sub set_url {
      return $urls[$url_index++];
    }


    Or something like that, but I don't know if that's what you're asking.


    [Edited by pwoneill on 03-27-2001 at 11:30 AM]

  3. #3
    Join Date
    Mar 2001
    Posts
    2
    This is it! And I need some additional information about how can I use condition IF under perl language, because I need to use the second URL only for one country Canada (this is financial sites), for others I still should use the old one.
    My problem is:
    these web pages have the same format, BUT old one has 2 columns - name of countries and rates, the new one has 6 columns - name of countries and 5 columns with rates for 5 days. I need to grab only first column and the sixth colums with the latest date.

    My question is:
    how should I exchange my code for it?

    My code is:

    #!/usr/local/bin/perl

    print "<--- EURO cron: EXCH --->\n";

    {

    use Date:ateCalc qw ( calc_new_date ) ;
    use HTTP::Request;
    use LWP::UserAgent;

    &set_date;

    print "file is $file\n";

    &set_url;

    print "url is $url\n";

    &get_url;

    $number_of_tries = 0;
    while ($found eq "FALSE" && $number_of_tries < 50) {
    &get_url; # set $found in this function.
    sleep 10; # sleep 10 seconds before trying again.
    $number_of_tries += 1;
    }

    }
    #
    #
    #
    sub set_url {
    $url = "http://www.ny.frb.org/pihome/statistics/forex12.shtml

    }
    #
    #
    #
    sub set_date {

    $yr = `date '+%y'`;
    $mth = `date '+%m'`;
    $day = `date '+%d'`;

    chop ( $yr );
    chop ( $mth );
    chop ( $day );

    $edat_base = $ENV{EDAT_BASE};
    $dir = "$edat_base/exch/data/";

    $file = $dir . $yr . $mth . $day . ".txt" ;

    }
    #
    #
    #
    sub get_url {

    $ua = new LWP::UserAgent;
    #$ua->proxy(http => $ENV{PROXY});
    $ua->agent("$0/0.1");
    $req = new HTTP::Request 'GET', $url;
    $req->header('Accept' => 'text/html');

    # send request
    $res = $ua->request($req);

    # check the outcome
    if ($res->is_success) {
    $fileout = ">".$file;
    print "$fileout\n";

    close (FILEOUT);
    open (FILEOUT,$fileout);
    print FILEOUT $res->content;

    $found = 'TRUE';

    } else {
    print "whoops\n";
    $found = 'FALSE';
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width