in reply to howto map/grep a complex structure

"i look forward to learn how to make it more elegant with map and grep."

You appear to be assuming that using map and/or grep will automatically make your code more elegant. This is not the case.

The code you already have is easy to read (and therefore maintain) and its intent is clear. I can't say the same for these map/grep alternatives:

print $_->[0] for grep { @{$_->[1]} } map { [ $_, [ grep { $_ eq $search } @{$hash->{$_}{member}} ] ] } keys %$hash;
print $_->[0] for grep { $_->[1] } map { [ $_, scalar grep { $_ eq $search } @{$hash->{$_}{member}} ] } keys %$hash;

Furthermore, for loops are often faster. Benchmark to check this with whatever alternative solution(s) you might be considering.

Here's my test code:

#!/usr/bin/env perl -l use strict; use warnings; my $hash = { A => { member => [qw{q w e}], }, B => { member => [qw{a s d}], }, C => { member => [qw{q a z}], }, }; my $search = 'q'; print 'OP Solution:'; for my $hk (keys %$hash) { for my $m (@{$hash->{$hk}{member}}) { if ($m eq $search) { print $hk; } } } print 'map/grep Solution 1'; print $_->[0] for grep { @{$_->[1]} } map { [ $_, [ grep { $_ eq $search } @{$hash->{$_}{member}} ] ] } keys %$hash; print 'map/grep Solution 2'; print $_->[0] for grep { $_->[1] } map { [ $_, scalar grep { $_ eq $search } @{$hash->{$_}{member}} ] } keys %$hash;

Output:

OP Solution: A C map/grep Solution 1 A C map/grep Solution 2 A C

— Ken