如何读取多个文本文件填充列计算统计perl

How to read multiple text file filled with columns calculating statistics perl

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

我想获得 Mean、Var 和 CoV 的统计数据,我遇到的问题是我真的不知道如何读取文本文件,因此我在正确的数组中获得了正确的值。

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

#!/usr/bin/perl

#foreach $line

$pkt_number  = ;
$pkt_arrival = ;
$pkt_size    = ;

$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)

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

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