next up previous contents
Next: Control Structures Up: Arrays and Lists Previous: Functions for Array and   Contents


Array Slices

Subscripts for arrays are not limited to scalars -- you can provide a list or array of subscripts, and extract what is known as an array slice. When you're extracting an array slice, it's important to make sure that the target to which you're assigning the slice is an array, and not a scalar, or the rules for an array in scalar context will be in effect.

When you extract an array slice, the subscript can be a literal list of values, an array, or a perl expression which will result in an array. Thus, the following statements will all extract an array slice consisting of the first three elements of the array values:

     @one = @values[0,1,2];
     @vals = (0,1,2);
     @two = @values[@vals];
     @three = @values[0..2];

You can also use an array slice on the left-hand side of an assignment statement, allowing you to replace multiple values in an array with a single statement.



Phil Spector 2002-10-18