You would then get an error about an uninitialized value. This happens because the hash slice creates a new key for '3' and sets its value to undef.

You will get a warning (if you're running with warnings enabled, that is), but only because you are using an uninitialized value, not because a new element is auto-vivified in the hash. If any of the keys in your hash slice does not exist, the hash slice will put an undef in that place in the resulting list. So in this case @months will get ('January', 'February', undef, 'April'), and you'll get a warning when you try to print the undef. But %month_hash remains the same, there is still no element with key '3'.

Auto-vivification can be a problem when you are referencing a nested structure, as in the following, albeit contrived, example:

my %HoA = ( '1' => [ 'January', 'Januare' ], '2' => [ 'February', 'Februare' ], '4' => [ 'April' , 'Avril' ], ); for (1 .. 4) { print "English: $HoA{$_}[0], French: $HoA{$_}[1]", $/; } # watch out! $HoA{3} has been auto-vivified as an ARRAY-ref!!

HTH

--
3dan

In reply to Re: Re: Re: What is a "slice" ? by edan
in thread What is a "slice" ? by Hayl_

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.