Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Extracting values from nested hashrefs

by choroba (Abbot)
on Sep 12, 2012 at 09:13 UTC ( #993195=note: print w/ replies, xml ) Need Help??


in reply to Extracting values from nested hashrefs

I am not sure whether map plus double values counts as implicit traversing:

say for map values %$_, values %$animals;
Update: simplified.
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Extracting values from nested hashrefs
Download Code
Re^2: Extracting values from nested hashrefs
by loris (Hermit) on Sep 12, 2012 at 09:35 UTC

    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

      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).
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      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://993195]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2014-09-30 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (384 votes), past polls