I hesitated to reply because I knew other monks would come up with a solution faster than I. I only would like to add grep for the possibility that the first array (@a1) contain non-keys as well.
Niggle :-)
This doesn't check for non-keys. It checks that each key has a defined value. In the process it auto-vivifies each key in @a1, which is probably not what you want even if you are searching for keys with defined values. For example, after running your example %h1 will be:
( 'key2' => 'bar', 'key1' => 'foo', 'not_a_key' => undef, )
Also, what if you want a key with an undef value?
If @a1 might contain non-keys they something like this will do the job.
@a2 = @h1{grep exists $h1{$_}, @a1};
In reply to Re^2: getting array of values from hash
by adrianh
in thread getting array of values from hash
by twinsen
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |