Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

What data structure is this code looking for

by mhearse (Hermit)
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' ) { } }

Comment on What data structure is this code looking for
Download Code
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.
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.


    Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2014-12-22 21:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (131 votes), past polls