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

Check for existance in a hash of hashes

by Perl_User (Acolyte)
on Mar 27, 2002 at 18:16 UTC ( #154744=perlquestion: print w/ replies, xml ) Need Help??
Perl_User has asked for the wisdom of the Perl Monks concerning the following question:

I am buidling a hash of hashes and passing that to a subroutine. In the subroutine, I am checking to see if the two keys exists or not. The code I have written is not working. Anyone please help.
my $skip_users = &get_data; &print_data; sub get_data { my %skip_users; #build a hoh with userid and value. $skip_users{$skip_id}{$value}++; return(\%skip_users) } sub print_data { my %hoh = %$skip_users; next if ((exists $hoh{$user}) && (exists $hoh{$user}{$command})); }
If I user the || instead of &&, it skips over the $hoh{$user}. But I want it to check for both the keys. I cannot seem to get the correct syntax for that.

Comment on Check for existance in a hash of hashes
Download Code
Re: Check for existance in a hash of hashes
by Fletch (Chancellor) on Mar 27, 2002 at 18:27 UTC
    DB<1> $a{foo}->{bar} = "zoikes" DB<2> print "yup\n" if exists $a{foo} yup DB<3> print "yup\n" if exists $a{foo}->{bar} yup DB<4> print "yup\n" if exists $a{foo}->{baz} DB<5> print "yup\n" if exists $a{foo} && exists $a{foo}->{bar} yup DB<6> print "yup\n" if exists $a{foo} && exists $a{foo}->{baz} DB<7>
      $a{foo}{bar} = 'blah'; print "Yep\n" if exists $a{foo} && $a{foo}{bar}; print "Nope\n" if exists $a{bar}{baz}; print "Whoops!\n" if exists $a{bar};

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        You've created $a{bar} by your second check, just as perldoc -f exists explains.

        Although the deepest nested array or hash will not spring into existence just because its existence was tested, any intervening ones will. Thus "$ref->{"A"}" and "$ref->{"A"}->{"B"}" will spring into existence due to the existence test for the $key element above. This happens anywhere the arrow operator is used, including even:...
Re: Check for existance in a hash of hashes
by moebius (Novice) on Mar 27, 2002 at 18:55 UTC
    Not sure exactly what wasn't working for you, but the following edit works per your specification.

    #!/usr/bin/perl
    
    my $skip_users = &get_data;
    &print_data;
    
    sub get_data
    {
        my %skip_users;
        my $skip_id = 'fred';
        my $value = 'ethel';
        #build a hoh with userid and value.
        $skip_users{$skip_id}{$value}++;
    
        return(\%skip_users);
    }
    
    sub print_data
    {
        my %hoh = %$skip_users;
        my $user = 'fred';
        my $command = 'thel';
        if ((exists $hoh{$user}) && (exists $hoh{$user}{$command})) {
            print "yep\n";
        } else {
            print "nope\n";
        }
    }
    
(Ovid) Re: Check for existance in a hash of hashes
by Ovid (Cardinal) on Mar 27, 2002 at 18:56 UTC

    Here's a general routine that will tell you if a particular branch of a hash exists. In otherwords, if you have this:

    $hash{ foo }{ bar }{ baz }

    It can be a pain to check for the existence of every single key.

    use strict; use warnings; use Data::Dumper; my %hash = ( one => { test => 3 }, two => { Ovid => { is => { a => 'fool' } } } ); if ( hash_branch_exists( \%hash, qw/ one test / ) ) { print "Good\n"; } else { print "Bad\n"; } if ( hash_branch_exists( \%hash, qw/ Ovid is a fool / ) ) { print "Good\n"; } else { print "Bad\n"; } if ( hash_branch_exists( \%hash, qw/ two Ovid Is a / ) ) { print "Good\n"; } else { print "Bad\n"; } sub hash_branch_exists { my ( $hash_ref, @fields ) = @_; foreach ( @fields ) { return 0 if ref $hash_ref ne 'HASH'; return 0 if ! exists $hash_ref->{ $_ }; $hash_ref = $hash_ref->{ $_ }; } return 1; }

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Check for existance in a hash of hashes
by PrakashK (Pilgrim) on Mar 27, 2002 at 19:03 UTC
    sub print_data { my %hoh = %$skip_users; next if ((exists $hoh{$user}) && (exists $hoh{$user}{$command})); }
    I wonder if the next if ... part is a typo. This should throw up an error unless print_data is called inside a loop.

    Do you mean:

    print $hoh{$user}{$command} if ((exists $hoh{$user}) && (exists $hoh{$user}{$command}));

    /prakash

Re: Check for existance in a hash of hashes
by trs80 (Priest) on Mar 28, 2002 at 04:51 UTC
    You might try defined instead of exists if you are more concerned with content then the key itself.
Re: Check for existance in a hash of hashes
by Perl_User (Acolyte) on Apr 01, 2002 at 19:56 UTC
    Thank you guys for all your help!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (20)
As of 2015-07-07 13:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls