Dear Monks,

1) From perldata/Slices:
A slice of an empty list is still an empty list. Thus: @a = ()[1,0]; # @a has no elements @b = (@a)[0,1]; # @b has no elements But: @a = (1)[1,0]; # @a has two elements @b = (1,undef)[1,0,2]; # @b has three elements More generally, a slice yields the empty list if it indexes only beyon +d the end of a list: @a = (1)[ 1,2]; # @a has no elements @b = (1)[0,1,2]; # @b has three elements

Why is indexing beyond the end of the list legal for a slice?

2) I think the slice docs should mention the case when you assign an empty list to a hash slice, like this:
use strict; use warnings; use 5.014; use Data::Dumper; my %hash = ('hello', 1, 'goodbye', 2, 'world', 3, 'mars', 4); @hash{'goodbye', 'world'} = (); say Dumper(\%hash); --output:-- $VAR1 = { 'mars' => 4, 'hello' => 1, 'world' => undef, 'goodbye' => undef };
It's not obvious(to me at least) why the value undef gets assigned to the sliced keys. It seems to me that the hash could just as well look like:
$VAR1 = { 'mars' => 4, 'hello' => 1, };
Maybe it's that way for hashes in order to be consistent with assigning an empty list to an array slice:
my @arr = (10, 20, 30, 40); @arr[1, 2] = (); say Dumper(\@arr); --output:-- $VAR1 = [ 10, undef, undef, 40 ];

In reply to Array slices: beyond the end/ Assigning an empty list to a Hash slice by 7stud

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.