I have a perl hash of hashes like the following:
$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 keys of members in the main hash that have FILE_C
defined. In the example, this will return only ID_1
.
I know how to do this in a cumbersome loop (iterating all keys, checking if FILE_C
is defined, if so — pushing the key to an array, finally returning this array), but I have a feeling there's a single-liner or even a function for this …
Yep, perl has the grep function:
my @keys = grep { defined $hash{$_}{FILE_C} } keys %hash;
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