Perl 获取数字数组的平均值的方法不正确

Perl 获取数字数组的平均值的方法不正确,perl,Perl,我有一个相当愚蠢的问题 输入数据: ______________________________________还有更多 我想得到每一百行末尾元素的平均值。 #!usr/bin/perl use utf8; while(<>){ if(/^No:\d{2}\s{3} \d\.\d{2}s{4} \d\.\d\s{3} \d{3}s{3} \d{3}s{3} \d{2}\.\d\s$/x){ chomp; pus

我有一个相当愚蠢的问题

输入数据:


______________________________________还有更多

我想得到每一百行末尾元素的平均值。

#!usr/bin/perl
use utf8;
while(<>){
    if(/^No:\d{2}\s{3}     \d\.\d{2}s{4}    \d\.\d\s{3}    \d{3}s{3}    \d{3}s{3}    \d{2}\.\d\s$/x){
        chomp;
        push @Array,[split/\s+/];
    }
}

foreach (0..$#Array/100){
    my $average=0;
    $average += $Array[$_][5]/100   foreach($_..$_+100);
    print "this is   $average \n";
}
**使用MS Excel**

25.9530
25.4850
25.7610
25.3160
25.1650
25.7060
25.9500
25.6800
25.4720
25.4820

你把数据看错了。在第一次传递时,你会看到值0-99,然后在第二次传递时,你会看到值1-100,然后是2-101,等等。听起来你想做0-99,然后是100-199,诸如此类,你可以这样做:

my $chunks = $#Array / 100;

foreach my $chunk (0 .. $chunks) {
    my $low = 100 * $chunk;
    my $high = $low + 100;
    $high = $#Array if $high > $#Array;
    my $sum = 0;
    foreach my $n ($low .. $high) {
         $sum += $Array[$n][5];
    }
    my $avg = $sum/($high - $low);
    print "avg=$avg\n";
}

您的问题到底是什么?大概您的问题是您的函数给出的值与Excel返回值不同。作为第一步,试着做一个小得多的问题,比如总共10行,每5行取一个平均值。然后您可以手动计算答案,以确定您的函数或(您使用的方式)Excel是否不正确。您在说什么?对于您提供的程序和数据,输出实际上是
,这是0
。请修正你的问题。
25.9530
25.4850
25.7610
25.3160
25.1650
25.7060
25.9500
25.6800
25.4720
25.4820
my $chunks = $#Array / 100;

foreach my $chunk (0 .. $chunks) {
    my $low = 100 * $chunk;
    my $high = $low + 100;
    $high = $#Array if $high > $#Array;
    my $sum = 0;
    foreach my $n ($low .. $high) {
         $sum += $Array[$n][5];
    }
    my $avg = $sum/($high - $low);
    print "avg=$avg\n";
}