Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://993233]
[Corion]: GrandFather: Ah, (hardware) APIs - I have a similar situation with Chrome and its API... It is fairly underdocumented and I guess I have to hunt...
[Corion]: ... down supposedly working code to find out what I'm supposed to do
[GrandFather]: I haven't any "working code" to inspect! I have to find ways to generate reference signals then check the numbers I get at the far end match.
[GrandFather]: That's ok when the signal is a voltage, but there are three axis accelerometers, gyroscopes and magnetometers in these things! A little invention is needed at times!
[Corion]: GrandFather: Yeah, in that aspect, hardware is far more a black box than software
[GrandFather]: Oh, and the manufacturer tells lies too! I'm sure they don't mean to, but I know for sure some of the stuff they say is just wrong. It's possible they don't understand what we are asking, but that's not a good look either
[Corion]: GrandFather: Ah, unit testing hardware is fun, especially when the hardware is uncalibrated ...
[GrandFather]: Us: "What scaling do we need to apply to the numbers from the SDK for the wibble?". Them: "Oh, the numbers from the SDK for the wibble are already correct, they don't need scaling"
[GrandFather]: In our code: wibble range 1 scale by 1, range 2 scale by 2, range 3 scale by 4, range 4 scale by 8

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2017-08-24 07:14 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (365 votes). Check out past polls.