Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Hash of Hashes: Get the next Key without foreach loop

by snape (Pilgrim)
on May 14, 2012 at 22:58 UTC ( #970527=perlquestion: print w/ replies, xml ) Need Help??
snape has asked for the wisdom of the Perl Monks concerning the following question:

Suppose, I have a hash of hashes which has three keys. I would like to know is it possible to go to know next key without using foreach loop. for example: I don't want to do the following

foreach my $key1 ( sort keys %hash){ foreach my $key2 ( sort keys %{$hash{$key1}){ foreach my $key2 ( sort keys %{$hash{$key1}{$key2} ){ + } } }

I am interested to know if I have the knowledge of 2 keys and not the third ( and we don't have multiple third keys), is it possible to know the value for the hash of hashes. Something like: (more of a crude way of doing)

foreach my $key1 ( sort keys %hash){ foreach my $key2 ( sort keys %{$hash{$key1}){ my $key3 = $hash{$key1}{$key2}; my $value = $hash{$key1}{$key2}{$key3}; } }

Sorry for confusion. Thanks.

Comment on Hash of Hashes: Get the next Key without foreach loop
Select or Download Code
Re: Hash of Hashes: Get the next Key without foreach loop
by chromatic (Archbishop) on May 15, 2012 at 01:32 UTC

    If you don't know the keys of a hash, you have to get them somehow. That means using keys, values, or each. There's no other good option. (You could always guess at what a hash contains, but that's not a good option.)


    Improve your skills with Modern Perl: the free book.

Re: Hash of Hashes: Get the next Key without foreach loop
by kcott (Abbot) on May 15, 2012 at 02:22 UTC

    You're going to run into problems with the second block of code you posted.

    You treat $hash{$key1}{$key2} as having a scalar, string value (for use as a hash key):

    my $key3 = $hash{$key1}{$key2};

    Then in the next line you treat the same value as a hash reference:

    my $value = $hash{$key1}{$key2}{$key3};

    You are effectively working on two different data structures:

    %hash = ( key1 => { key2 => $some_key } ); %hash = ( key1 => { key2 => { $some_key => $some_value } } );

    -- Ken

Re: Hash of Hashes: Get the next Key without foreach loop
by Khen1950fx (Canon) on May 15, 2012 at 02:37 UTC
    Your script reminds me of a Jackson's song. "Simple as 1-2-3, A-B-C!". It's a pneumonic that I've used to help me remember hash slices, which is what I believe that you're looking for.
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper::Concise; my %hash = (1 => 'A', 2 => 'B', 3 => 'C'); print "The first and second keys: hash slice ==> \n", Dumper( @hash{qw(1 2)} ); print "Just the third key: \n", Dumper( $hash{3} );
    Note that I used @hash{qw(1 2)} to get at multiple keys; also, the keys must be quoted in slices.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-08-29 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (279 votes), past polls