How do I know if array values ​​are hash keys in Perl?

I have a say @array array. I would like to know which array values ​​form the hash keys, say% hash. Is there an easy way to do this other than using a for loop?

eg.

@array = qw (abc); %hash = ( a => 1, b=> 2 ); 

In this case, it should simply output "a" and "b".

+7
source share
1 answer

This should do it:

 my @array = qw(abc) ; my %hash = ( a => 1 , b => 2 ) ; my @result = grep { exists $hash{$_} } @array ; 
+17
source

All Articles