Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Hash of Array of Hashes

by heisenbug (Sexton)
on Apr 27, 2011 at 21:15 UTC ( [id://901658]=perlquestion: print w/replies, xml ) Need Help??

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

I have a nested data mess that I am recieving from another program, and I am trying to extract data from it.

I have a hash table called "Data" where one of the keys is "logins". This key "logins" has a value pair that is an array. This array is an array of hashes. The hash includes a key called url and a value of a url I need.

I would like to extract the value of a url in the array. Any idea how to extract this?

Replies are listed 'Best First'.
Re: Hash of Array of Hashes
by wind (Priest) on Apr 27, 2011 at 21:41 UTC
      Thank you for the Perl data structure cookbook. Your code did not work but it sure gave me a place to look how to solve this. Since this was nested I needed to refrence it with arrows. Thanks so much for your help. I am bookmarking this link too. Here is the code that worked.
      print $Data->{logins}->[0]->{url};

        Only the first (leftmost) arrow would make a difference, the others are always optional (as they don't help to disambiguate — those are always references).  In other words, you also could've written:

        print $Data->{logins}[0]{url};
Re: Hash of Array of Hashes
by Tanktalus (Canon) on Apr 27, 2011 at 21:20 UTC

    Can you give an example?

    Generally, I find that spitting a deep structure out using Data::Dumper (or even Data::Dump) can make complicated queries like this much more obvious. Have you tried that?

      I don’t much care for Data::Dumper’s look. I much prefer the output from the Dumpvalue module for this sort of thing. It’s what the Perl debugger uses. Data::Dumper is a serialization module, whereas Dumpvalue’s job is to show you the data structure in a good-looking way. That doesn’t mean it has to be eval()able.
Re: Hash of Array of Hashes
by planetscape (Chancellor) on Apr 28, 2011 at 05:28 UTC

    The answers to How can I visualize my complex data structure? can help you understand what your complex data structure looks like, and this understanding will lead to knowing how to access a given element within.

    HTH,

    planetscape
Re: Hash of Array of Hashes
by nvivek (Vicar) on Apr 28, 2011 at 05:06 UTC

    You explain with an example data. Now I tried with an example hash according to your description. If I understood your hash structure wrongly, you give the example data.

    use strict; use warnings; use Data::Dumper; my %hash=("logins" => [ { '1' => 'http://www.perlmonks.com/?node_id=90 +1658' , '2' => 'http://www.google.com' }, { '3'=> 'http://www.bksyste +ms.co.in'}]); #prints the structure of the hash print Dumper \%hash; print "==========================\n"; # getting the length of an array and going through each index of an ar +ray for (0 .. length @{$hash{'logins'}}) { for my $key (keys%{$hash{'logins'}[$_]}) { # printing key and value print "Key: $key and Value:$hash{'logins'}[$_]{$key}\n"; + } print "==========================\n" }
Re: Hash of Array of Hashes
by sundialsvc4 (Abbot) on Apr 28, 2011 at 12:40 UTC

    Short, specific examples would be nice.

    The key notion that you need to wrap your mind around is references.   (See perlref.)

    A “reference” is a Thing, that “refers to” another Thing somewhere else.   So when you have “an array of hashes,” the more proper way to say it is that you have “an array of hashrefs.”   (The word, “hashref,” is simply a shorthand way of saying, “a reference to a hash.”)   So, the data structure itself, is over here, and there are lots of “references to” that data structure scattered about in other places.   (Think about that for a little while, until the light-bulb pops on.   And I don’t mean that to sound condescending.)   It’s very much like “pointers,” but considerably more sturdy.

    Arbitrarily complex data-structures can be built using this mechanism.   You might need to read up on weaken in Scalar::Util, but that’s another idea for another day...

    There are several ways to write this code, and all of those ways work equally well.   It’s quite important to use use strict and use warnings when writing this kind of code, but otherwise, Perl is quite generous and forgiving in its syntax rules.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-03-28 17:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found