[英]How to find if values of an array are the keys of hash in Perl?
I have an array say @array. 我有一个数组说@array。 I would like to know which values of the array form the keys of a hash, say %hash.
我想知道数组的哪些值构成哈希的键,比如%hash。 Is there is a simple way to do it other than using a for loop?
除了使用for循环之外,还有一种简单的方法吗?
eg, 例如,
@array = qw (a b c);
%hash = ( a => 1, b=> 2 );
In this case it should just output 'a' and 'b'. 在这种情况下,它应该只输出'a'和'b'。
This should do it: 这应该这样做:
my @array = qw(a b c) ;
my %hash = ( a => 1 , b => 2 ) ;
my @result = grep { exists $hash{$_} } @array ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.