Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Autovivification saves a lot of annoying effort. So if $quick is originally empty
$quick->{brown}->{fox} ->{$jumped} = 1 / $Lazy_dog;
creates the intermediate hashes so $quick can point to a hash-of-hashes-of-hashes.

But autovivification can bite you / surprise you, too. If you are testing a value (rather than setting it) autovivification can still add entries to your hash. That is, new keys. E.g., if you start with

$stateCityPopulation = {Illinois => {Springfield => 116482}, Massachusets => {Springfield => 154082}, Missouri => {Springfield => 150797}}};
and then you
foreach my $state (qw(Illinois Massachusets Ohio Missouri)){ my $pop = $stateCityPopulation->{$state}->{Springfield} || next; $springfielders += $pop}
Now when you look at keys %$stateCityPopulation, 'Ohio' is in there. It's holding an empty array. It was created when you were trying to access the value for $stateCityPopulation->{Ohio}->{Springfield}. Even though you got undef back as a value, the itermediate array got created 'by side effect.'

If you're debugging code, and you find an array has a bunch of unexpected keys, which hold empty hashes, then autovivification is to blame. If you'd changed the code in the above to

$pop = ($stateCityPopulation->{$state} && $stateCityPopulation->{$state}->{Springfield}) || next;
You'd have skipped the autovivification (at the expense of messier code.)

BTW, if you're debugging a rogue autovivification, the 'w' switch in the debugger is very handy.


In reply to Re: Explaining Autovivication by throop
in thread Explaining Autovivication by perly-gates

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (5)
    As of 2020-10-27 18:57 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (257 votes). Check out past polls.