Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Extracting values from nested hashrefs

by trizen (Hermit)
on Sep 12, 2012 at 13:39 UTC ( #993227=note: print w/replies, xml ) Need Help??

in reply to Extracting values from nested hashrefs

Using recursion:
use 5.010; my $animals = { gnu => { humps => 0, mascot_for => 'emacs', }, dromedary => { humps => 1, zzz => { humps => 'nested', aaa => { humps => 'really nested', xxx => 0, }, }, mascot_for => 'perl', }, }; sub print_humps { my $hash_ref = shift; foreach my $key (keys %{$hash_ref}) { if (ref $hash_ref->{$key} eq "HASH") { print_humps($hash_ref->{$key}); } elsif ($key eq 'humps') { say $hash_ref->{$key}; } } } print_humps($animals); __END__ 0 1 nested really nested

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://993227]
[Lady_Aleena]: Hello everyone. I'm having a blonde moment. I can push an array to an array, right? push @to_array, @another_array;
[shmem]: of course
[shmem]: the members of @another_array are conflated to @array
[shmem]: try it out:
[Lady_Aleena]: shmem, thanks. I'm am way out of practice.
[shmem]: perl -lE '@foo = (0..3);@bar = (4..7); push @foo,@bar; say for @foo'
[Lady_Aleena]: shmem, I feel like an idiot for forgetting something so basic.
shmem puts a big cauldron of "silly con charme" on the table in the refectorium

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2017-04-27 11:31 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (503 votes). Check out past polls.