kum
kum

Reputation: 21

In Perl, how do you access an element from a hash of hashes

in this example I want to read the letter "d" from $ref:

$ref={a,b,c,{d,e}}

Upvotes: 1

Views: 205

Answers (2)

FMc
FMc

Reputation: 42421

# Start using these!
use strict;
use warnings;

# A more standard way of writing your example.
my $ref = { a => "b", c => { d => "e", f => "g" } };

# How to access elements within the structure.
my $inner = $ref->{c};
print $_, "\n" for
    $inner->{d},   # e
    keys %$inner,  # d f
    $ref->{c}{d},  # e    (directly, without using intermediate variable).
;

For more info, see the Perl Data Structures Cookbook.

Upvotes: 4

Quentin
Quentin

Reputation: 944429

print keys %{$ref->{c}}; will work for that specific (awful) example. It may or may not solve your problem since we don't know what the problem actually is.

Upvotes: 2

Related Questions