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
--In reply to Re: Re: Re: What is a "slice" ?
by edan
in thread What is a "slice" ?
by Hayl_
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |