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 "BambooWeb"), 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.

[edit]

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).

[edit]

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.

This article is a stub. You can help BambooWeb by expanding it (http://en.BambooWeb.org/w/index.php?title=Array_slicing).


-This article has been brought to you by BambooWeb and Wikipedia-



  View Live Article   This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License