将输出数据格式化为excel文件

时间:2013-10-07 02:01:54

标签: perl loops data-structures escaping output

此程序从Web获取@values数组中每个值的数值 我希望这些值打印在一个看起来像

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

其中v1 .. v12是@values等中第一个变量的值。这里是我的程序,请帮我构建它。是否有一个转义字符可以让我回到perl程序的第一行 感谢

  #!/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);

1 个答案:

答案 0 :(得分: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";
}