我在这段Perl代码中犯了什么错误?
我试过这个密码。这不管用。我的输出没有结果。我犯了什么错误我在这段Perl代码中犯了什么错误?,perl,hash,Perl,Hash,我试过这个密码。这不管用。我的输出没有结果。我犯了什么错误 my %fruit_color = ("apple", "red", "banana", "yellow"); my @fruits = keys %fruit_colors; my @colors = values %fruit_colors; print @fruits; print @colors; 代码板链接:您声明的是水果颜色,但引用的是水果颜色(注意后面的S) 如果你使用警告和严厉的话,你会注意到这一点 use warni
my %fruit_color = ("apple", "red", "banana", "yellow");
my @fruits = keys %fruit_colors;
my @colors = values %fruit_colors;
print @fruits;
print @colors;
代码板链接:您声明的是
水果颜色
,但引用的是水果颜色
(注意后面的S)
如果你使用警告和严厉的话,你会注意到这一点
use warnings;
use strict;
Global symbol "%fruit_colors" requires explicit package name at C:\temp\test.pl line 4.
Global symbol "%fruit_colors" requires explicit package name at C:\temp\test.pl line 5.
您有一个输入错误:
my %fruit_color = ("apple", "red", "banana", "yellow");
应该是
my %fruit_colors = ("apple", "red", "banana", "yellow");
第一个错误是没有将此作为第一行:
use strict;
use warnings;
其次,您有一个输入错误(如果您使用的是strict模块,那么会更容易发现)。您的代码中有一个输入错误,如果您有:
use warnings;
use strict;
在你的代码中。你应该一直这么做。伙计,你们这些人很快 正如其他人所说:
- 您使用一个名称创建了一个列表,但随后将其引用到另一个名称
- 如果使用了
和use warnings
,则会收到一条错误消息,告诉您该错误use strict
因此,请继续阅读Perldoc中的最佳实践部分(您知道
Perldoc
文档,不是吗?输入命令Perldoc
,看看您得到了什么)并吸收其中的知识。那就去拿康威的书。愚蠢的错误:p谢谢你的快速回复:)