Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

What data structure is this code looking for

by mhearse (Chaplain)
on Nov 15, 2011 at 15:08 UTC ( #938193=perlquestion: print w/replies, xml ) Need Help??
mhearse has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to figure out what type of data structure this code is looking for:
foreach my $key (keys %{$abc}) { if ( ref $abc->{$key} eq 'HASH' ) { } }

Replies are listed 'Best First'.
Re: What data structure is this code looking for
by wfsp (Abbot) on Nov 15, 2011 at 15:21 UTC
    A hash of hashes (HoH)
    #! /usr/perl/bin use warnings; use strict; use Data::Dumper; my $abc = { one => { oneone => 11, onetwo => 12, }, two => { twoone => 21, twotwo => 22, } }; foreach my $key (keys %{$abc}) { if ( ref $abc->{$key} eq 'HASH' ) { for my $subkey (keys %{$abc->{$key}}){ printf qq{%s -> %s\n}, $subkey, $abc->{$key}{$subkey}; } } }
    onetwo -> 12 oneone -> 11 twotwo -> 22 twoone -> 21
      Thanks for the replies. I actually figured this out moments after the post. Is it possible to delete my own posts?
        Nooo! Don't delete it. It's a good question and will be useful for others facing the same problem.
        The post is doing no harm. It might help other users searching on this site for helpful threads. You can edit your posts, but if you remove the text the answers to your post would hang there without meaning

Re: What data structure is this code looking for
by davido (Archbishop) on Nov 15, 2011 at 16:18 UTC

    The other posts provide good insight, but if you really want to know what a data structure looks like one of the simplest methods is to do this:

    use Data::Dumper; # ....... print Dumper( $abc );

    You'll get a dump of the data structure on your screen. Very useful for getting a snapshot of what you're working with. Sometimes I'm even the one who created the structure, and I will use Data::Dumper to verify that it looks as I expected it to.


Re: What data structure is this code looking for
by Anonymous Monk on Nov 15, 2011 at 15:21 UTC
    It's looking for keys in the $abc hashref whose corresponding value is a hashref.
    my $abc = { 1 => 2, 3 => [4, 5,], 6 => {7 => 8, 9 => 10,}, };
    The condition in your code will be true for key 6, but false for the others, because their values are a plain scalar and an arrayref, correspondingly.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://938193]
Approved by davies
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2018-06-18 11:37 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.