in reply to Check for existance in a hash of hashes

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>

Replies are listed 'Best First'.
Re2: Check for existence in a hash of hashes
by dragonchild (Archbishop) on Mar 27, 2002 at 18:46 UTC
    $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:...
        I know that. You know that. Did the original poster know that? Did all the readers of this thread know that?

        When you post a solution, you should post the common caveats, gimmes, and gotchas as well. Otherwise, those who may not be as fluent as the saints will get frustrated.

        ------
        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.