"Autivivification" is a fancy word for "don't let me mess up if stuff isn't there yet".

In hashes, autovivification creates the hash entry you were looking for and sets it to undef; if you try to continue down a chain of references to other anonymous data structures, autovivification creates them (empty) and continues.

So If you said

$pinky = {}; $pinky->{zorch}{poit}{narf} = "I don't know, Brain, where will we get +hip-waders this time of night?";
what logically happens is this:
$pinky = {}; # Ooh, you referenced zorch, and it's not there. $pinky->{zorch} = undef; # And now you want to reference 'poit' in that as a hash, so we'll add + an new anonymous hash instead: $pinky->{zorch} = {}; $pinky->{zorch}{poit} = undef; # and then you want to go one more level: $pinky->{zorch}{poit} = {}; $pinky->{zorch}{poit}{narf} = undef; # And you want to set that to a value: $pinky->{zorch}{poit}{narf} = "I don't know, Brain, where will we get +hip-waders this time of night?";
Note that Perl does not actually keep doing and undoing the assignments of undef; it just plows through, building the structures as it goes. That's autovivification. Note that if you just access something at the end of one of these chains, you'll get undef; Perl does set the value to undef if you don't assign it anything, just as it would if you referenced a key in a hash that hadn't been set yet.

Also note that this

$eat_memory={}; $eat_memory->{bad_idea}[99999][99999][99999] = "yummy, yummy, memory";
forces autovivification to create all those unused entries in the arrays, so we have 'bad_idea' referencing a 100,000 element anonymous array, the last element of which references a 100,000 element anonymous array, the last element of which references another 100,000 element array, the last element of which is set to our string. Using hashes unless you absolutely have to use arrays will let you simulate a sparse array.

In reply to Re: Autovivification in perl by pemungkah
in thread Autovivification in perl by rammohan

Title:
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, details, 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, summary, 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.