Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

accessing a hash of hashes

by existem (Sexton)
on Feb 15, 2005 at 11:28 UTC ( #431114=perlquestion: print w/replies, xml ) Need Help??

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

Very simple question for you, i've had a look at previous posts and documentation but can't work out how to do this. I have no problems creating a big multidimensional hash, but accessing it is a whole other story. This is the structure I have created.

$urls{'network1'}{'id'}='whatever'; $urls{'network2'}{'id'}='whatever'; $urls{'network3'}{'id'}='whatever';

I tried just looping through the keys of %urls, but then get stuck about how to access the values of the second level of the hash, i.e. the whatever bit. Any nice

foreach my $key (keys %urls) { }

style solution would be perfect. Thanks, Tom

Replies are listed 'Best First'.
Re: accessing a hash of hashes
by monkey_boy (Priest) on Feb 15, 2005 at 11:59 UTC

    use strict; my %urls; $urls{'networkid'}{'someidIdontknow'}='somevalue1'; $urls{'networkid2'}{'someidIdontknow2'}='somevalue2'; $urls{'networkid3'}{'someidIdontknow3'}='somevalue3'; foreach my $network (keys %urls) { foreach my $key (keys %{$urls{$network}}) { print "$network\t$key\t$urls{$network}{$key}\n"; } }

    Im so sick of my Signature...
Re: accessing a hash of hashes
by tomhukins (Curate) on Feb 15, 2005 at 11:34 UTC

    Take a look at Perl's perlreftut and perlref documentation.

    Each item in %urls contains a hash reference. To access this hash reference as a hash, use %$key. To access the value of a key directly from a hash reference, use the -> operator.

Re: accessing a hash of hashes
by Thilosophy (Curate) on Feb 15, 2005 at 11:37 UTC
    foreach my $key (keys %urls) { print $urls{$key}{id}; }

      Thanks for the reply, that would work for my example, but I should have explained that I don't actually know the value of ID all the time, so infact my structure might look more like this:

      $urls{'networkid'}{'someidIdontknow'}='somevalue1'; $urls{'networkid2'}{'someidIdontknow2'}='somevalue2'; $urls{'networkid3'}{'someidIdontknow3'}='somevalue3';

      So unfortunately I can't access the value I don't know directly, but need a means to loop through all the values.

      From looking at the post above I now have this code but it doesn't work.

      foreach my $network (keys %urls) { foreach my $key (keys %$network) { print "$key<br/>"; } }

      Any ideas where i'm going wrong? I know it's something to do with accessing the hashref.

      Cheers, Tom

        foreach my $network (keys %urls) { foreach my $key (keys %{$urls{$network}}) { print "$key<br/>"; } }
        foreach my $network (values %urls) { foreach my $key (keys %$network) { print "$key<br/>"; } }
Re: accessing a hash of hashes
by holli (Abbot) on Feb 15, 2005 at 11:41 UTC
    my %hash = ( foo => { bar => baz } ); print $hash{foo}{bar};
    In you code, $key is equiv. with foo.

    holli, /regexed monk/

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2022-08-10 20:52 GMT
Find Nodes?
    Voting Booth?

    No recent polls found