Perl Global Symbol需要显式包名称错误

时间:2014-10-06 22:46:35

标签: perl

为什么这个Perl代码会给我一个错误?

my $next_maximum = 0;
my $corresponding_key;
my $count_to_ten = 0;
my %top10words; #the new hash
while (count_to_ten < 10) {
  $next_maximum = 0;
  $corresponding_key = "";
  while (my ($key, $value) = each(%words)) {
    if ($next_maximum<$value) {
      $next_maximum = $value;
      $corresponding_key = $key;
    }
  }
  delete $words{$corresponding_key}; #remove the maximum from the original word list
  $top10words($corresponding_key) = $next_maximum;
  $count_to_ten++;
}

$top10words($corresponding_key) = $next_maximum;给出的错误为Global symbol "$top10words" requires explicit package name at lab2.pl。我不明白这意味着什么,但我认为这是由于范围的变化。我觉得我已经将变量声明在适当的范围内,所以我被卡住了。

1 个答案:

答案 0 :(得分:2)

$top10words($corresponding_key)

应该是

$top10words{$corresponding_key}

错误是因为您(无意中)使用了未声明的标量$top10words,而不是哈希%top10words