Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Access Complex Hash

by Anonymous Monk
on May 15, 2012 at 11:09 UTC ( #970602=note: print w/ replies, xml ) Need Help??


in reply to Access Complex Hash

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.


Comment on Re: Access Complex Hash
Select or Download Code
Re^2: Access Complex Hash
by Athanasius (Vicar) on May 15, 2012 at 13:07 UTC

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

    Output:

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

    HTH,

    Athanasius <°(((><contra mundum

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://970602]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (15)
As of 2013-05-22 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best material for plates (tableware) is:









    Results (457 votes), past polls