Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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 drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2017-07-27 19:57 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (422 votes). Check out past polls.