如何读取多个文本文件,用于计算统计perl的列

时间:2015-04-29 11:07:52

标签: perl statistics measurement

我正在运行一些网络测量,根据这张图片我现在得到了大量的时间戳: http://i.stack.imgur.com/TNnBf.png

我想得到Mean,Var&的统计数据。 CoV和我遇到的问题是我不知道如何阅读文本文件,所以我在正确的数组中得到了正确的值。

我看到Bash在这方面被认为很差,所以我认为perl应该更好。这是我到目前为止的尝试:

#!/usr/bin/perl

#foreach $line

$pkt_number  = $1;
$pkt_arrival = $2;
$pkt_size    = $6;

$nr_of_pkg                = 16;
$linespeed                = 100000;
$sum_of_throughput        = 0;
$throughput               = 0;
$sum_of_throughputsquared = 0;
$mean                     = 0;
$var                      = 0;
$co_v                     = 0;

$duration = $pkt_arrival[0] - $pkt_arrival[15];

for (my $i = 0 ; $nr_of_pkg > $pkt_number[i] ; $i++) {
  $throughput[i]             = $packet_size[i] / $pkt_arrival[i];
  $sum_of_throughput         = $sum_of_throughput + $throughput[i];
  $sum_of_throughput_squared = $sum_of_throughput_squared + $throughput[i] * $throughput[i];
}

$mean = $sum_of_throughput / $nr_of_pkg;
$var  = (($sum_of_throughput * $sum_of_throughput) - $sum_of_throughput_squared);
$co_v = sqrt($var)

1 个答案:

答案 0 :(得分:0)

这是读取和解析文本文件的代码。

my @pkt_number;
my @pkt_arrival;
my @pkt_size;

my $i = 0;
my $line;
while ( <> ) { 
    $line = " $_"; # make sure it always start with a whitespace
    chomp $line;
    ( $pkt_number[$i], $pkt_arrival[$i], $pkt_size[$i] )
        = (split( /\s+/, $line, 8 ))[ 1, 2, 6 ];
    printf "[%s]\n", join("/", $pkt_number[$i], $pkt_arrival[$i], $pkt_size[$i]);
    $i++;
}

这就是你在Linux上运行它的方式:

% cat result.txt | ./your_perl.pl