Firstly, a slice is just a set of elements of an array. In other words, if you want the first 5 and last 5 members of a class of 100 people, you would write:

my @smalllist = @biglist[0 .. 4, 95 .. 99];

Note that while using a slice you access an array as a list (@) rather than a scalar($).

Using a slice keeps the @biglist the same as when you started. If you want to trim off the @biglist to eliminate the people in the @smalllist from it, you could use a splice() function call to do it.

my @smalllist = splice(@biglist,0,5), splice(@biglist,-5,5);

Mostly, the reason that you would use splice() is just because it's a quick way of getting rid of the elements of a list as you use them, especially when you're using them in a unusual order or in sizes other than 1.

The Offset means where you start your splice from, and either refers to a position away from the start of the list (if you use a positive number) or away from the end of the list (if you use a negative number).

Length is how many elements you're going to cut away from the list.

Hope this helps,
Dave.

P.S.
When I was first learning Perl, I was very excited about how flexible the functions were, and I read quite a bit of the perlfunc document. You might want to try that. Afterwards, though, I read Programming Perl (which I still don't fully understand). It's a challenging read, but worth it. If you only read up on the functions, you won't see the full power of the language.

You can read about the functions here: http://www.perldoc.com/perl5.8.0/pod/perlfunc.html


In reply to Re: array splicing by David Caughell
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.