更新时间:2021-09-23 22:21:26
对于 Perl one-liner,它与 Ayman Hourieh 的回答:
For a Perl one-liner, it's basically the same thing as the awk
solution in Ayman Hourieh's answer:
% perl -nle '$sum += $_ } END { print $sum'
如果您对 Perl one-liners 的作用感到好奇,您可以解析它们:
If you're curious what Perl one-liners do, you can deparse them:
% perl -MO=Deparse -nle '$sum += $_ } END { print $sum'
结果是程序的一个更冗长的版本,以一种没人会自己编写的形式:
The result is a more verbose version of the program, in a form that no one would ever write on their own:
BEGIN { $/ = "
"; $ = "
"; }
LINE: while (defined($_ = <ARGV>)) {
chomp $_;
$sum += $_;
}
sub END {
print $sum;
}
-e syntax OK
只是为了傻笑,我用一个包含 1,000,000 个数字(在 0 - 9,999 范围内)的文件进行了尝试.在我的 Mac Pro 上,它几乎立即恢复.这太糟糕了,因为我希望使用 mmap
会非常快,但它只是在同一时间:
Just for giggles, I tried this with a file containing 1,000,000 numbers (in the range 0 - 9,999). On my Mac Pro, it returns virtually instantaneously. That's too bad, because I was hoping using mmap
would be really fast, but it's just the same time:
use 5.010;
use File::Map qw(map_file);
map_file my $map, $ARGV[0];
$sum += $1 while $map =~ m/(d+)/g;
say $sum;