Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: Extracting values from nested hashrefs

by loris (Hermit)
on Sep 12, 2012 at 09:35 UTC ( #993199=note: print w/replies, xml ) Need Help??


in reply to Re: Extracting values from nested hashrefs
in thread Extracting values from nested hashrefs

Ah, sorry, my example was too simple. It should have been like this:

my $animals = { gnu => { humps => 0, mascot_for => 'emacs', }, dromedary => { humps => 1, mascot_for => 'perl', }, camel => { humps => 2, mascot_for => 'perl', }, };

BTW, implicit traversing is OK; I just wanted to avoid some sort of loop with , say while.

Thanks,

loris

Replies are listed 'Best First'.
Re^3: Extracting values from nested hashrefs
by choroba (Chancellor) on Sep 12, 2012 at 09:45 UTC
    You can use
    map $_->{humps}, values %$animals;
    If your structure becomes even more complicated (e.g. different level of nesting), you should use recursion (or CPAN).
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^3: Extracting values from nested hashrefs
by Athanasius (Chancellor) on Sep 12, 2012 at 12:46 UTC

    Here is an approach that should work no matter how complicated your data structure becomes (provided the data you want remains in the form humps => d):

    #! perl use strict; use warnings; use Data::Dumper; my $animals = { gnu => { humps => 0, mascot_for => 'emacs', }, dromedary => { humps => 1, mascot_for => 'perl', }, camel => { humps => 2, mascot_for => 'perl', }, }; my $flat = Dumper($animals); print "$1\n" while $flat =~ / 'humps' \s+ => \s+ (\d+) /gx;

    Output:

    2 0 1

    TMTOWTDI.

    Athanasius <°(((><contra mundum

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://993199]
help
Chatterbox?
[zentara]: s/thread/cpu/
[marto]: xkcd://538
[Discipulus]: these opcodes mentioned in the wiki pages are the same opcode perl is translated into? or is just the same term but in different fields?
[zentara]: it's useful if you want to lockup a multicore machine :-)
[zentara]: marto I would give up the key easy with drugs....
[Corion]: Discipulus: No, the Perl opcodes are more high-level than the raw processor opcodes
[zentara]: sodium pentathol and a beautiful woman interogator :-)
[Discipulus]: thanks Corion if so i probably never meet them in my life.. ;=)

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2017-07-28 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (428 votes). Check out past polls.