I often use splice to grab more than one item out of a list at a time. E.g. instead of:
for my $x (@list) { do something with $x }
you can do
while (my ($x,$y) = splice(@list, 0, 2)) { do something with $x and $y }
Of course, the latter destroys the list as it iterates over it.

Did you have specific questions about offset and length that aren't answered in the fine manual?

With the above while loop, changing to splice(@list, -2) will grab $x and $y off the end of the array instead of the beginning (though that way will die if @list has an odd number of elements instead of just getting an undef $y).

May I suggest a super search of splice to see many different ways of using it?

Update: said slice where meant splice.


In reply to Re: array splicing by ysth
in thread array splicing by Anonymous Monk

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.