Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

help to get hash elements

by danny0085 (Sexton)
on Aug 26, 2012 at 21:37 UTC ( #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' => '', 'name' => 'equipo', 'updated' => 1346015230 }, { 'url' => '', 'name' => 'linuxmint', 'updated' => 1345967468 } ], }, };
I did not find a good tutorial to handle complex data structures, please give me a hand

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

    my %hash = ( response => { blogs => [ { url => '', name => 'equipo', updated => 1346015230, }, { url => '', 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).


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' => '', 'name' => 'equipo', 'updated' => 1346015230 }, { 'url' => '', 'name' => 'linuxmint', 'updated' => 1345967468 } ], }, }; say $_->{url} for @{$h->{response}->{blogs}};


    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}; } }


    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?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2015-11-29 15:09 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (751 votes), past polls