解决方法
如果我明白你,也许你不需要一个零数组;相反,你需要一个哈希。散列键将是另一个数组中的值,散列值将是另一个数组中值存在的次数:
use strict; use warnings; my @other_array = (0,1,2,3,4); my %tallies; $tallies{$_} ++ for @other_array; print "$_ => $tallies{$_}\n" for sort {$a <=> $b} keys %tallies;
输出:
0 => 3 1 => 1 2 => 2 3 => 3 4 => 1
要更直接地回答您的具体问题,要创建一个填充一堆零的数组,您可以在这两个示例中使用该技术:
my @zeroes = (0) x 5; # (0,0) my @zeroes = (0) x @other_array; # A zero for each item in @other_array. # This works because in scalar context # an array evaluates to its size.