Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Extracting values from nested hashrefs

by tobyink (Abbot)
on Sep 12, 2012 at 14:38 UTC ( #993233=note: print w/ replies, xml ) Need Help??


in reply to Extracting values from nested hashrefs

Here are four ways of doing it...

use strict; use warnings; use Test::More; use B::Deparse; my $animals; my $deparse = B::Deparse->new; $deparse->ambient_pragmas(strict => 'all', warnings => 'all'); # Function to check that each solution works! sub this_works (&) { $animals = { gnu => { humps => 0, }, dromedary => { humps => 1, }, camel => { humps => 2, }, }; my @results = $_[0]->(); my $code = $deparse->coderef2text($_[0]); if (is_deeply [sort @results], [qw/ 0 1 2 /] ) { diag "this works $code"; } else { diag "FAILED! $code"; } } this_works { map($_->{humps}, values %$animals) }; this_works { map { $_->{humps} } values %$animals }; this_works { map { $animals->{$_}{humps} } keys %$animals }; this_works { require JSON::Path; JSON::Path->new('$..humps')->values($animals); }; done_testing();
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Extracting values from nested hashrefs
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2014-09-15 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (145 votes), past polls