Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Access Complex Hash

by perl@1983 (Sexton)
on May 15, 2012 at 10:55 UTC ( #970598=perlquestion: print w/ replies, xml ) Need Help??
perl@1983 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, Can someone please tell me as to how to access "key3" and "key4" in below hash?
$hash = { key1 => { key2 => { [ key3 => 'Hello' ], { key4 => 'World' }, } } };
Thanks in advance.

Comment on Access Complex Hash
Download Code
Re: Access Complex Hash
by Anonymous Monk on May 15, 2012 at 11:09 UTC
    You can't because this is a bizarre non-sensical data structure. The part [ key3 => 'Hello' ] is a hash key. It will be coerced to its string representation, e.g. ARRAY(0x759de8). When dumped again, we see something like:
    $hash = { key1 => { key2 => { "ARRAY(0x759de8)" => { key4 => 'World' } } } };
    I suggest you think again about how you construct this structure, there must be a mistake somewhere.

      Anonymous Monk is correct, you can’t access key3 or 'Hello' (see perlref).

      However, for the record, key4 and 'World' can be accessed as follows:

      #!/usr/bin/perl use strict; use warnings; my $hash = { key1 => { key2 => { [ key3 => 'Hello' ], { key4 => 'World' }, } } }; my ($value2) = values $hash->{key1}{key2}; my ($key4, $value4) = each %$value2; print "\$key4 = '$key4', \$value4 = '$value4'\n";


      $key4 = 'key4', $value4 = 'World'


      Athanasius <°(((><contra mundum

Re: Access Complex Hash
by sundialsvc4 (Monsignor) on May 15, 2012 at 12:48 UTC

    On the other hand, if we assume that the square brackets in key3 are a typographic error, as it probably is, then the answer to the question would be straightforward, such as:

    print $$hash{'key1'}{'key2'}{'key3'}; ==> will print "Hello"

    If you want to iterate through the structure, you might use the keys function, and, since this function requires a hash argument, you might have to tell Perl explicitly that this is what it is, e.g. noting the underlined text below:
    keys %{$$hash{'key1'}{'key2'}}

    Finally, note that $$hash{'key'} is a shorthand, a favorite of mine, that also could have been written $hash->{'key'}.

    Notice that in my example the hash-keys are string literals as they must be.   If you had omitted the quotes, Perl would interpret them as so-called barewords, and, if you did not use use strict; use warnings;, you might not be advised of the issue.   Perl might interpret them as (undefined...) variables and produce nothing useful.

    Data::Dumper is always your friend.

    HTH ...

      Thanks for the detailed explanation! It was really helpful!

        Happy to be of service.

        Now, your number one take-away from all this should be ... use strict; use warnings; !!

        Perl is one of those very-powerful tools in which “the price you pay” is that sometimes it is not immediately obvious what it is actually doing, because it tends to assume that you do understand.   While this is a perfectly valid approach for The Implementors to have taken, it can also spell a lot of frustration “when, in fact, you don’t. understand.”   (That certainly was the case for me!   So, mind you, I am not being “an apologist” here!   Merely a bringer of Fair Warning...)   HTH.™

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970598]
Front-paged by Corion
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 2014-07-12 20:23 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (241 votes), past polls