I have an array say @array. I would like to know which values of the array form the keys of a hash, say %hash. Is there is a simple way to do it other than using a for loop?
e.g.,
@array = qw (a b c);
%hash = ( a => 1, b=> 2 );
In this case it should just output 'a' and 'b'.
This should do it:
my @array = qw(a b c) ;
my %hash = ( a => 1 , b => 2 ) ;
my @result = grep { exists $hash{$_} } @array ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With