Fast perl hash filtering

I have a hash of perl hashes as shown below:

$VAR1 = { 'ID_1' => { 'FILE_B' => '/path/to/file/file1', 'FILE_C' => '/path/to/file/file2', 'FILE_A' => '/path/to/file/file3' }, 'ID_2' => { 'FILE_B' => '/path/to/file/file4', 'FILE_A' => '/path/to/file/file5' }, 'ID_3' => { 'FILE_B' => '/path/to/file/file6', 'FILE_A' => '/path/to/file/file7' } ... } 

I would like to get a list of all member keys in the main hash that have FILE_C . In the example, this will return only ID_1 .

I know how to do this in a cumbersome loop (iterating over all keys, checking if FILE_C is FILE_C , if so - pressing a key into an array, finally returning this array), but I have a feeling that one is a -liner or even a function for this...

+6
filter perl hash
source share
1 answer

Yep, perl has a grep function:

my @keys = grep { defined $hash{$_}{FILE_C} } keys %hash;

+15
source share

All Articles