johngg explained with code, let me try with words. You are correct that @indexes doesn't change. After the splice, your @arr has been reduced to the elements ('one','three','four','five'); I hope how splice works is clear? Then, @arr[@indexes] is attempting to access array indices 0 through 4. That works fine for the indices 0 through 3, which returns all of the remaining elements of @arr. Index 4 no longer exists in @arr, so instead, undef is returned (which is the normal behavior when accessing an array index that doesn't exist) - in other words, @arr[0..4] evaluates to ('one','three','four','five',undef). When you print those values, undef will evaluate to the empty string, and if warnings are enabled, issue a warning. This is why the suggestions from the AM post are good: always Use strict and warnings, and as per the Basic debugging checklist, when in doubt, use Data::Dumper or Data::Dump to show your variables, since they will more clearly show you undef (or whitespace in strings, etc.).


In reply to Re: splice and indexes by haukex
in thread splice and indexes by teun-arno

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.