我有一个简单的哈希表,并想根据$value的条件返回$key。也就是说,对于第14行,我需要什么代码才能返回$value为"yellow"时对应的$key?
1 #!/usr/bin/perl
2
3 # This program creates a hash then
4 # prints out what is in the hash
5
6 %fruit = (
7 'apple' => ['red','green'],
8 'kiwi' => 'green',
9 'banana' => 'yellow',
10 );
11
12 print "The apple is @{$fruit{apple}}.\n";
13 print "The kiwi is $fruit{kiwi}.\n";
14 print "What is yellow? ";
print "$_ " foreach @matching_keys
最好改写为print "@matching_keys"
,并无额外空格。此外,codaddict是正确的,grep 在数组引用的值上不起作用。 - TLP