Array slicing
|
In computer programming, array slicing is an operation that extracts certain elements from an array and packages them as another array, possibly with different number of indices and different index ranges. Two common examples are extracting a substring from a string of characters (e.g. "kipe" from "wikipedia"), and extracting a row (or a column) of a rectangular matrix to be used as a vector.
Depending on the language and context, the elements of the new array may be aliased to (i.e., share memory with) those of the original array.
Slicing vectors and strings
For "one-dimensional" (single-indexed) arrays — vectors, sequence, strings etc. — the most common slicing operation is extraction of zero or more consecutive elements. Thus, if we have a vector containing elements (2, 5, 7, 3, 8, 6, 4, 1), and we want to create an array slice from index 2 to 5, we get (7, 3, 8, 6).
In the Perl programming language, which allows arrays to be sliced easily, if we have
@x = (2, 5, 7, 3, 8, 6, 4, 1)
as above,
@x[2..5]
will represent the sliced array (7, 3, 8, 6).
History
The concept of slicing was surely known even before the invention of compilers. Slicing as a language feature probably started with the FORTRAN, more as a consequence of non-existent type and range checking than by design. Fortran 90 and Kenneth Iverson's APL provide very flexible multi-dimensional array slicing, which contribute much to the languages' expressive powers.