【发布时间】:2016-06-05 06:11:12
【问题描述】:
我有一个文件,其中包含一些带有浮点数的行,如下所示:
1.66278886795044
1.61858296394348
1.66523003578186
1.62150096893311
1.6725389957428
我正在尝试打开该文件,计算所有行的平均值,然后将平均值写入同一个文件。其中5 代表项目的数量,因此预期输出为:
1.66278886795044
1.61858296394348
1.66523003578186
1.62150096893311
1.6725389957428
================
AVERAGE after 5 runs: 1.6481283664703
考虑到我可以从脚本$amountofloops 中更高的变量中获取运行次数,我认为这样做可以:
open(my $overviewhandle, '>>', 'overview.txt');
chomp( my @durations = <$overviewhandle> );
my $avgtime = 0;
foreach my $duration (@durations) {
$avgtime += $duration;
}
$avgtime = $avgtime / $amountofloops;
print $overviewhandle "================\nAVERAGE after $amountofloops runs: $avgtime";
close $overviewhandle;
但是,$avgtime 一直返回零,我不知道为什么。我认为这个数字没有被解析为数字。
【问题讨论】: