Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6361037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:43:29+00:00 2026-05-24T23:43:29+00:00

Sorry, this seems like such a basic question but I still don’t understand. If

  • 0

Sorry, this seems like such a basic question but I still don’t understand. If I have a hash, for example:

my %md_hash = ();
$md_hash{'top'}{'primary'}{'secondary'} = 0;

How come this is true?

if ($md_hash{'top'}{'foobar'}{'secondary'} == 0) {
    print "I'm true even though I'm not in that hash\n";
}

There is no “foobar” level in the hash so shouldn’t that result in false?

TIA

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:43:30+00:00Added an answer on May 24, 2026 at 11:43 pm

    Try a search on “Perl autovivification”.

    The hash values “spring into existence” when you first access them. In this case, the value is undef, which when interpreted as a number is zero.

    To test for existence of a hash value without auto-vivifying it, use the exists operator:

    if (exists $md_hash{'top'}{'foobar'}{'secondary'}
        && $md_hash{'top'}{'foobar'}{'secondary'} == 0) {
        print "I exist and I am zero\n";
    }
    

    Note that this will still auto-vivify $md_hash{'top'} and
    $md_hash{'top'}{'foobar'} (i.e. the sub-hashes).

    [edit]

    As tchrist points out in a comment, it is poor style to compare undef against anything. So a better way to write this code would be:

    if (defined $md_hash{'top'}{'foobar'}{'secondary'}
        && $md_hash{'top'}{'foobar'}{'secondary'} == 0) {
        print "I exist and I am zero\n";
    }
    

    (Although this will now auto-vivify all three levels of the nested hash, setting the lowest level to undef‘.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry, this seems like such an obvious question, but I just can't find an
Sorry for asking what seems like such an obvious question. I have an adapter
Sorry if this seems like a stupid question but can't find any info on
Sorry this is such a basic question, but I can't find a straight answer
Ok sorry this might seem like a dumb question but I cannot figure this
Sorry if this sounds like a really stupid question, but I need to make
Sorry I know similar question have been asked many times before but like most
This may seem like a silly question if so sorry =] Ok i have
Sorry for what seems like a silly question: But I've never, ever worked with
Sorry if this question seems trivial to many here. In a C++ code there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.