我试过这段代码.这不起作用.我输出没有结果.我犯了什么错误?
my %fruit_color = ("apple", "red", "banana", "yellow");
my @fruits = keys %fruit_colors;
my @colors = values %fruit_colors;
print @fruits;
print @colors;
Run Code Online (Sandbox Code Playgroud)
您正在声明fruit_color但引用fruit_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.
Run Code Online (Sandbox Code Playgroud)
第一个错误是没有这个作为第一行:
use strict;
use warnings;
Run Code Online (Sandbox Code Playgroud)
其次,你有一个拼写错误(如果你一直使用严格的模块,这将更容易发现).