perl – 按值的简单哈希搜索
发布时间:2020-12-15 21:18:16 所属栏目:大数据 来源:网络整理
导读:我有一个简单的哈希,并希望以$ value标准返回$ key。也就是说,对于第14行,我需要返回$ $“$”的$键的代码? 1 #!/usr/bin/perl23 # This program creates a hash then4 # prints out what is in the hash56 %fruit = (7 'apple' = ['red','green'],8 'kiw
我有一个简单的哈希,并希望以$ value标准返回$ key。也就是说,对于第14行,我需要返回$ $“$”的$键的代码?
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? "; 解决方法
grep是这项工作的正确工具:
my @all_matches = grep { $fruit{$_} eq 'yellow' } keys %fruit; print("$_ ") foreach @matching_keys; my ($any_match) = grep { $fruit{$_} eq 'yellow' } keys %fruit; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |