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

help to get hash elements

by danny0085 (Sexton)
on Aug 26, 2012 at 21:37 UTC ( [id://989881]=perlquestion: print w/replies, xml ) Need Help??

danny0085 has asked for the wisdom of the Perl Monks concerning the following question:

I need to get all the "url" elements from:
$VAR1 = { 'response' => { 'blogs' => [ { 'url' => 'equipo.tumblr.com', 'name' => 'equipo', 'updated' => 1346015230 }, { 'url' => 'linux.tumblr.com', 'name' => 'linuxmint', 'updated' => 1345967468 } ], }, };
I did not find a good tutorial to handle complex data structures, please give me a hand

Replies are listed 'Best First'.
Re: help to get hash elements
by davido (Cardinal) on Aug 26, 2012 at 22:52 UTC

    my %hash = ( response => { blogs => [ { url => 'equipo.tumblr.com', name => 'equipo', updated => 1346015230, }, { url => 'linux.tumblr.com', name => 'linuxmint', updated => 1345967468, }, ], }, ); my @urls = map { $_->{url} } @{$hash{response}{blogs}};

    perldoc perlreftut and peldoc perlref can get you started with references. perldsc and perllol can walk you through manipulating datastructures built using references. I used map (perldoc -f map) to perform the actual data structure transform. But if you're new to some of this it might be easier to achieve the same thing using a foreach loop (perldoc perlsyn) and push (perldoc -f push):

    my @urls; foreach my $blog ( @{$hash{response}{blogs}} ) { push @urls, $blog->{url}; }

    Either way @url ends up containing the same list of URL's.

    If you don't mind spending a little on a good book, Intermediate Perl is a great tutorial on references, data-structures, classes, and objects (as well as a few other useful things).


    Dave

Re: help to get hash elements
by philiprbrenan (Monk) on Aug 26, 2012 at 23:16 UTC
    use feature ":5.14"; use warnings FATAL => qw(all); use strict; use Data::Dump qw(dump); my $h = { 'response' => { 'blogs' => [ { 'url' => 'equipo.tumblr.com', 'name' => 'equipo', 'updated' => 1346015230 }, { 'url' => 'linux.tumblr.com', 'name' => 'linuxmint', 'updated' => 1345967468 } ], }, }; say $_->{url} for @{$h->{response}->{blogs}};

    Produces

    equipo.tumblr.com linux.tumblr.com

    The ->{} ->[] operators can be used to position yourself within the structure and for @{} and for keys %{} can be used to iterate through parts of it.

Re: help to get hash elements
by linuxer (Curate) on Aug 26, 2012 at 22:13 UTC

    What about Access and Printing of a HASH OF HASHES?

    Simply create a 2-level loop and iterate through the keys of each level.

    # untested: my @urls; for my $outer ( keys %hash ) { for my $inner ( keys %{ $hash{$outer} } ) { # maybe check before, if entry "url" exists or is defined ... push @urls, $hash{$outer}->{$inner}->{url}; } }

    update:

    I missed last night that there is an anonymous array inside $hash{$outer}->{$inner}; so here's an updated code:

    for my $outer ( keys %hash ) { for my $inner ( keys %{ $hash{$outer} } ) { push @urls, map { $_->{url} } @{ $hash{$outer}->{$inner} } } }

    As the other answers point out, if there is only one "outer" and one "inner" key, you can skip the loops and access the inner referenced array directly...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://989881]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found