0

This program gets numeric values from the web for each of the values in my @values array I want these values to be printed out in a table which looks like

       il9  il8 il7
 2012  v1    b1
 2011  v2    b2
 2010  v3    b3
   .
   .
 2000  v12   b12

where v1 .. v12 are values for the first variable in @values etc. here is my program please help me structure it. Is there an escape character that could take me back to the first line of the program in perl thanks

  #!/usr/bin/perl -w
  use strict;
  use LWP::UserAgent;
  use URI;
  my $browser = LWP::UserAgent->new;
  $browser->timeout(10);
  $browser->env_proxy;

  open(OUT, ">out");
  my $i = 2013;
  while ($i-- > 2000){print OUT "$i\n"}
  my $a = 2013 ;
  my $base = 'http://webtools.mf.uni-lj.si/public/summarisenumbers.php';
  my @values = ('il9', 'il8', 'il6' );
  foreach my $value (@values) {
print OUT "$value \n"
    while ($a-- > 2000){
                my $b = $a + 1;
                my $c = $b + 1; 
                my $query = '?query=('.$value.')'.$a.'[dp] NOT           '.$b.'[dp] NOT '.$c.'[dp]';
                my $add = $base.$query;
                #my $url = URI->new($add);  
                #my $response = $browser->get($url); 
                #if($response->is_success) {print OUT $response->decoded_content;}
                #else {die $response->status_line};
                print OUT "$query\n";
                } $a = 2013; print OUT
                        }

   close(OUT);
4

1 回答 1

0

更加注意格式/缩进和变量命名 - 这会对你有很大帮助。

#!/usr/bin/perl

use strict;
use warnings;
use LWP::UserAgent;

my $base_url  = 'http://webtools.mf.uni-lj.si/public/summarisenumbers.php';
my @values    = ( 'il9', 'il8', 'il6' );
my $stat_data = {};

my $browser = LWP::UserAgent->new;
$browser->timeout(10);
$browser->env_proxy;

for my $value ( @values ) {

    for my $year ( 2010 .. 2013 ) {

        my $query = '?query=(' . $value . ')' . $year .'[dp] NOT           ' . ($year+1) . '[dp] NOT ' . ($year+2) .'[dp]';
        my $url   = "$base_url$query";

        my $response = $browser->get( $url );

        if( $response->is_success ) {
            ## store the fetched content in a hash structure
            $stat_data->{$year}->{$value} = $response->decoded_content;
        }
        else {
            die $response->status_line;
        }
    }
}

## print the header
print "\t", join( "\t", @values ), "\n";

## print the data by the years in reverse order
for my $year ( reverse sort keys %{$stat_data} ) {

    print $year;

    for my $value ( @values ) {
        print "\t", $stat_data->{$year}->{$value};
    }

    print "\n";
}
于 2013-10-07T17:38:50.693 回答