尝试使用 for 循环打印水果及其颜色。
use strict;
my %colors = ( apple => 'red',
orange => 'orange',
watermelon => 'green',
grapes => 'blue',
rest => 'pink' );
for (keys %colors) {
print("color of $_ is $colors($_)\n");
}
最佳答案
$colors($_)
必须是 $colors{$_}
访问哈希值需要使用{ }
关于perl - 执行以下代码片段时。我可以看到错误“全局符号 "$colors"需要显式包名称”。必须打印键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71424119/