Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: A more elegant way to filter a nested hash?

by shmem (Chancellor)
on May 31, 2018 at 10:15 UTC ( [id://1215534]=note: print w/replies, xml ) Need Help??


in reply to Re: A more elegant way to filter a nested hash?
in thread A more elegant way to filter a nested hash?

map can't handle recursion.

Of course you can have a map block inside a function, and recurse into that from inside the map block:

sub hash_filter { my $source = shift; my $filter = shift; return { map { ref $filter->{$_} eq 'HASH' ? ref $source->{$_} eq 'HASH' ? ($_, hash_filter( $source->{$_}, $filter->{$_} )) : croak "bad filter: on '$_', expected HASH\n" : ($_, $source->{$_}) } grep { exists $source->{$_} } keys %$filter } }

which btw is the for loop of the OP rewritten in terms of map/grep.

update: a terse version which eliminates shifting @_ and working directly on the arguments (which aren't altered by the function):

sub hash_filter { return { map { ref $_[1]->{$_} eq 'HASH' ? ref $_[0]->{$_} eq 'HASH' ? ($_, hash_filter( $_[0]->{$_}, $_[1]->{$_} )) : croak "bad filter: on '$_', expected HASH\n" : ($_, $_[0]->{$_}) } grep { exists $_[0]->{$_} } keys %{$_[1]} } }

Now that's arguably micro-optimized and far less readable (11 lines) than the OP's code (18 lines) imho.

More elegant? Perhaps for those who prefer nested ?: statements for simple expressions over if/else blocks...

update 2: B::Concise shows identical optrees (execpt line numbers) for the "for" and "map/grep" solutions.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

Replies are listed 'Best First'.
Re^3: A more elegant way to filter a nested hash?
by jimpudar (Pilgrim) on May 31, 2018 at 20:41 UTC

    Thanks for the example of how this can be done. I was scratching my head trying to figure out how to handle the recursion with map.

    πάντων χρημάτων μέτρον έστιν άνθρωπος.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1215534]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (8)
As of 2024-04-18 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found