Perhaps it would help to consider a related situation.

I can almost see it making some sense to have $blah[-1]= $x; create a last element (a form of autovivification) and then set it. But if that were true than what should $blah[-2]= $y; do? Should it create a second-to-last element? So if @blah only contains one element, it would be the same as unshift @blah, $y; ? And if @blah is empty, it would be the same as @blah= ( $y, undef ); ?

Going to such extremes of autovivification stretches the imagination a bit more and is harder to justify/swallow. But I can't see drawing the line between -1 and -2 (or between any other particular pair of negative numbers) so either we'd always autovivify or never. Things get more bizarre when you consider all of the cases for $blah[-3]= $z;.

So, if you like, you could write up a patch that causes such assignments to unshift undef onto the array until the index makes sense. It kinda makes sense and even seems rather Perlish. I'm more looking for the option of having less autovivification than more, but some might like the idea. There's even a chance your patch might get accepted. I'm not sure how big of a chance...

        - tye

In reply to (tye)Re: array index -1 oddity by tye
in thread array index -1 oddity by shemp

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.