Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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
[LanX]: I want the command line in the history
[tye]: -Mouse
[Corion]: Option a) would mean launching cmd.exe /k c:\path\to\ batchfile- launching-perl- script.cmd. Option b) would be to add pause as the last line of said batch file.
[LanX]: First day after holidays ... and already stressed by the fact that colleagues changed stuff without communication ... apparently I'm the only one trying to fight entropy
[Corion]: LanX: The command is always in the history if you typed it in before. If you didn't type the command into the command line, it will not be there. I think there is doskey which can stuff command lines into the history
LanX damns the cult of CB ;-)
[LanX]: please forget my last 3 posts
[LanX]: Yeah option a doesn't go into history
[LanX]: probably I need to teach the app to restart after C-c Kill

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2017-03-27 15:48 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.