Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

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


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

Is there a simpler way to do this with higher order functions like map and grep?

Of course you can transform a for-loop into a map/grep construct, populating the return value \%output in one fell stroke (why isn't that a reference in the first place, if that is what is returned?) - but I doubt you would save significant lines of code, or improve readability. Job well done, move on :-)

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

Replies are listed 'Best First'.
Re^2: A more elegant way to filter a nested hash?
by Veltro (Hermit) on May 31, 2018 at 18:28 UTC

    Why is this getting so much up votes. People still advertising how to slice hashes using map and grep when the Perl language has more elegant ways to slice hashes.

    use strict ; use warnings ; use Data::Dumper ; my $source = { f1 => 'garbage', f2 => 'more garbage', f3 => 'important data', f4 => { this => 'sub hash', is => 'garbage' }, f5 => { f6 => 'more important data', f7 => { more => 'garbage', f8 => 'important data', }, f9 => 'garbage', }, f10 => [ 'important', 'data' ], f11 => [ 'more', 'garbage' ] }; my $filter = { f3 => 1, f5 => { f6 => 1, f7 => { f8 => 1 } }, f10 => 1 }; sub sliceTheWholeDarnThing { my $s = $_[0] ; my $f = $_[1] ; my $n = {} ; my @keysToGet = keys %{ $f } ; @{ $n }{ @keysToGet } = @{ $s }{ @keysToGet } ; foreach ( keys %{ $n } ) { if ( ref $n->{ $_ } eq 'HASH' ) { $n->{ $_ } = sliceTheWholeDarnThing( $s->{ $_ }, $f->{ $_ +} ) ; } } return $n ; } my $newHash = sliceTheWholeDarnThing( $source, $filter ) ; print Dumper( $newHash ) ;
      Why is this getting so much up votes.

      Reasons may include:

      1. first answer
      2. pointing out that foreach or map/grep are essentially the same thing (seee below)
      3. telling the OP that a working and readable solution is enough
      4. eating my own dog food by not writing too much (but see below)

      Hash slices are syntactic sugar for iterating over a set of keys. I'd suspect the code path for both being identical (see B, do benchmarks with a meaningful set). Elegance is in the eye of the beholder.

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

        Excuse me, but what you are calling syntactic sugar, I call grammar!. Actually I find your comment diminishing towards all those fantastic computer linguistics that have been working so hard to incorporate iterative behaviors into the natural language of Perl. Choosing long used idioms over major parts of the grammar is your choice but personally I feel you are staying one step behind. Take Perl 6 for example where they are taking these concepts even a 1000 steps further!

        Just doing a bit of googling, finding tons of examples. This one from the perl6 archives is already from 2000:

        The ability to take an indexed slice of a hash is desired. This would allow the programmer to pare out several keys and values from hash A into a new hash B, for greatest flexibility. Currently, this is only available through map():

            %other = map { $_ => $sounds->{$_} } qw(lizard duck);

        Which could be simplified to:

        %other = slice(%$sounds, { qw(lizard duck) }); # or, %other = (%$sounds =~ sl/lizard duck/h); # or, %other = %$sounds->{'lizard', 'duck'}; # or, %other = %{$sounds}{ qw(lizard duck) }; # trad'l

        Syntactic sugar? Sure, whatever.

        edit: corrected typo, changed last comment.

      Thanks for the input!

      I do agree this is quite a bit more elegant. Hash slicing is very nicely suited to this problem.

      I will need to put together a set of test cases for this function anyways, so I will try out all the versions posted so far and report back on the Benchmark results.

      Best,

      Jim

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

      I had the same idea, but you are effectively iterating twice over the keys if you slice first.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        I don't think so. I am iterating over the 'selected' keys twice.

        2018-06-09 Athanasius restored original content

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-23 18:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found