首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

帮忙看看这代码为啥运行后一点反应都没,读取文件形成数组然后将数组中的数据相加解决办法

2012-05-03 
帮忙看看这代码为啥运行后一点反应都没,读取文件形成数组然后将数组中的数据相加#!/usr/bin/perlopen(FD,

帮忙看看这代码为啥运行后一点反应都没,读取文件形成数组然后将数组中的数据相加
#!/usr/bin/perl
open(FD,"C:/ex12042713.log")||die("Can not open the file!$!n");
@array=<FD>;
close(FD);

my @array;
while(<>){
  push @array, (split / /)[4] unless ( /^([#]|\s+)/ ); #只处理开头不是'#'及空格的行
}

my $sum = 0;
$sum += $_ foreach @array;
print $sum;




文件是ex12042713.log 
文件内容
#Software: Microsoft Internet Information Services 5.1
#Version: 1.0
#Date: 2012-04-27 05:07:20
#Fields:  
  date time c-ip s-ip sc-bytes cs-bytes time-taken  
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 28547 323 109
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 30416 392 47
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 15423 377 47
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 16300 389 15
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 3350 375 15
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 2820 374 16
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 6074 387 16
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 94267 384 172
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 48224 393 94
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 57753 393 62
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 2092 647 0
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 50689 393 16
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 62793 393 0
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 59863 393 31
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 3974 372 0
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 4154 372 15
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 3575 372 16
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 4304 372 16
2012-04-27 05:07:20 192.168.155.236 192.168.155.17 3906 372 0

[解决办法]

Perl code
open(FD,"C:/ex12042713.log")||die("Can not open the file!$!n");foreach (<FD>){  push @array, (split / /)[4] unless ( /^([#]|\s+)/ ); #只处理开头不是'#'及空格的行}close(FD); 

热点排行