Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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.

Replies are listed 'Best First'.
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 (Chancellor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970527]
Approved by kcott
[Corion]: Hurr. Yesterday I played around with ffmpeg as a new toy and found its "scene" filter great - it detects scene changes. Now I could write a module that splits a given video on its cuts into different scenes. Except I have no use case for that :)
[Corion]: (and also, writing yet another FFmpeg module just to wrap system() and grep through its output isn't all that great ...)
[erix]: cut out advertisements from movies? :)
[erix]: robably not possible (or it would have been done already)

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2018-05-24 11:14 GMT
Find Nodes?
    Voting Booth?