Array slicing
Encyclopedia
In computer programming
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

, array slicing is an operation that extracts certain elements from an array and packages them as another array, possibly with different number of indices (or dimensions
Dimensions
Dimensions is a French project that makes educational movies about mathematics, focusing on spatial geometry. It uses POV-Ray to render some of the animations, and the films are release under a Creative Commons licence....

) and different index ranges. Two common examples are extracting a substring from a string of characters (e.g. "ell" from "hello"), and extracting a row (or a column) of a rectangular matrix
Matrix (mathematics)
In mathematics, a matrix is a rectangular array of numbers, symbols, or expressions. The individual items in a matrix are called its elements or entries. An example of a matrix with six elements isMatrices of the same size can be added or subtracted element by element...

 to be used as a vector.

Depending on the programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 and context, the elements of the new array may be aliased to
Aliasing (computing)
In computing, aliasing describes a situation in which a data location in memory can be accessed through different symbolic names in the program. Thus, modifying the data through one name implicitly modifies the values associated to all aliased names, which may not be expected by the programmer...

 (i.e., share memory with) those of the original array.

Details

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 the 3rd to the 6th items, we get (7, 3, 8, 6). In programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

s that use a 0-based indexing scheme, the slice would be from index 2 to 5.

Reducing the range of any index to a single value effectively eliminates that index. This feature can be used, for example, to extract one-dimensional slices (vectors) or two-dimensional slices (rectangular matrices) from a three-dimensional array. However, since the range can be specified at run-time, type-checked languages may require an explicit (compile-time) notation to actually eliminate the trivial indices.

General array slicing can be implemented (whether or not built into the language) by referencing every array through a dope vector
Dope vector
In computer programming, a dope vector is a data structure used to hold information about a data object, e.g. an array, especially its memory layout....

 or descriptor — a record that contains the address of the first array element, and then the range of each index and the corresponding coefficient in the indexing formula. This technique also allows immediate array transposition
Transpose
In linear algebra, the transpose of a matrix A is another matrix AT created by any one of the following equivalent actions:...

, index reversal, subsampling, etc. For languages like C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

, where the indices always start at zero, the dope vector of an array with d indices has at least 1 + 2d parameters. For languages that allow arbitrary lower bounds for indices, like Pascal, the dope vector needs 1 + 3d entries.

If the array abstraction does not support true negative indices (as for example the arrays of Ada
Ada (programming language)
Ada is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages...

 and Pascal
Pascal (programming language)
Pascal is an influential imperative and procedural programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small and efficient language intended to encourage good programming practices using structured programming and data structuring.A derivative known as Object Pascal...

 do), then negative indices for the bounds of the slice for a given dimension are sometimes used to specify an offset from the end of the array in that dimension. In 1-based schemes, -1 generally would indicate the second-to-last item, while in a 0-based system, it would mean the very last item.

History

The concept of slicing was surely known even before the invention of compiler
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...

s. Slicing as a language feature probably started with FORTRAN
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...

 (1957), more as a consequence of non-existent type and range checking than by design. The concept was also alluded to in the preliminary report for the IAL
ALGOL 58
ALGOL 58, originally known as IAL, is one of the family of ALGOL computer programming languages. It was an early compromise design soon superseded by ALGOL 60...

 (ALGOL 58) in that the syntax allowed one or more indices of an array element (or, for that matter, of a procedure call) to be omitted when used as an actual parameter.

Kenneth Iverson
Kenneth E. Iverson
Kenneth Eugene Iverson was a Canadian computer scientist noted for the development of the APL programming language in 1962. He was honored with the Turing Award in 1979 for his contributions to mathematical notation and programming language theory...

's APL
APL programming language
APL is an interactive array-oriented language and integrated development environment, which is available from a number of commercial and noncommercial vendors and for most computer platforms. It is based on a mathematical notation developed by Kenneth E...

 (1957) had very flexible multi-dimensional array slicing, which contributed much to the language's expressive power and popularity.

ALGOL 68
ALGOL 68
ALGOL 68 isan imperative computerprogramming language that was conceived as a successor to theALGOL 60 programming language, designed with the goal of a...

 (1968) introduced comprehensive multi-dimension array slicing and trimming features.

Array slicing facilities have been incorporated in several modern languages, such as Ada 2005
Ada (programming language)
Ada is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages...

, Boo
Boo programming language
Boo is an object-oriented, statically typed, general-purpose programming language that seeks to make use of the Common Language Infrastructure's support for Unicode, internationalization, and web applications, while using a Python-inspired syntax and a special focus on language and compiler...

, Cobra
Cobra (programming language from Cobra Language LLC)
Cobra is an object-oriented programming language produced by Cobra Language LLC. Cobra is designed by Chuck Esterbrook, and runs on the Microsoft .NET and Mono platforms. It is strongly influenced by Python, C#, Eiffel, Objective-C, and other programming languages. It supports both static and...

, D, Fortran 90
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...

, Go
Go (programming language)
Go is a compiled, garbage-collected, concurrent programming language developed by Google Inc.The initial design of Go was started in September 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. Go was officially announced in November 2009. In May 2010, Rob Pike publicly stated that Go was being...

, Matlab, Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

, Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

, S-Lang, Windows PowerShell
Windows PowerShell
Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework...

 and the mathematical/statistical languages GNU Octave, S
S programming language
S is a statistical programming language developed primarily by John Chambers and Rick Becker and Allan Wilks of Bell Laboratories...

 and R.

1966: Fortran 66

The Fortran 66 programmers were only able to take advantage of slicing matrices by row, and then only when passing that row to a subroutine
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

:
SUBROUTINE PRINT V(VEC, LEN)
REAL VEC(*)
PRINT *, (VEC(I), I = 1, LEN)
END
 
PROGRAM MAIN
PARAMETER(LEN = 3)
REAL MATRIX(LEN, LEN)
DATA MATRIX/1, 1, 1, 2, 4, 8, 3, 9, 27/
CALL PRINT V(MATRIX(1, 2), LEN)
END

Result:
2. 4. 8.
Note that there is no dope vector
Dope vector
In computer programming, a dope vector is a data structure used to hold information about a data object, e.g. an array, especially its memory layout....

 in FORTRAN 66 hence the length of the slice must also be passed as an argument - or some other means - to the SUBROUTINE. 1970s Pascal
Pascal (programming language)
Pascal is an influential imperative and procedural programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small and efficient language intended to encourage good programming practices using structured programming and data structuring.A derivative known as Object Pascal...

 and C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 had similar restrictions.

1968: Algol 68
ALGOL 68
ALGOL 68 isan imperative computerprogramming language that was conceived as a successor to theALGOL 60 programming language, designed with the goal of a...

Algol68 final report contains an early example of slicing, slices are specified in the form:
[lower bound:upper bound] ¢ for computers with extended character sets ¢
or
(LOWER BOUND..UPPER BOUND) # FOR COMPUTERS WITH ONLY 6 BIT CHARACTERS. #
Both bounds are inclusive and can be omitted, in which case they default to the declared array bounds. Neither the stride facility, nor diagonal slice aliases are part of the revised report.

Examples:
[3, 3]real a := ((1, 1, 1), (2, 4, 8), (3, 9, 27)); # declaration of a variable matrix #
[,] real c = ((1, 1, 1), (2, 4, 8), (3, 9, 27)); # constant matrix, the size is implied #
 
ref[]real row := a[2,]; # alias/ref to a row slice #
ref[]real col2 = a[, 2]; # permanent alias/ref to second column #
 
print ((a[:, 2], newline)); # second column slice #
print ((a[1⌈a, :], newline)); # last row slice #
print ((a[:, 2⌈a], newline)); # last column slice #
print ((a[:2, :2], newline)); # leading 2-by-2 submatrix "slice" #

+1.000010+0 +4.000010+0 +9.000010+0
+3.000010+0 +9.000010+0 +2.700010+1
+1.000010+0 +8.000010+0 +2.700010+1
+1.000010+0 +1.000010+0 +2.000010+0 +4.000010+0

1970s: MATLAB
MATLAB
MATLAB is a numerical computing environment and fourth-generation programming language. Developed by MathWorks, MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages,...

/GNU Octave
GNU Octave
GNU Octave is a high-level language, primarily intended for numerical computations. It provides a convenient command-line interface for solving linear and nonlinear problems numerically, and for performing other numerical experiments using a language that is mostly compatible with MATLAB...

/Scilab
Scilab
Scilab is an open source, cross-platform numerical computational package and a high-level, numerically oriented programming language. Itcan be used for signal processing, statistical analysis, image enhancement, fluid dynamics simulations, numerical optimization, and modeling and simulation of...

> A = round(rand(3, 4, 5)*10) # 3x4x5 three-dimensional or cubic array
> A(:, :, 3) # 3x4 two-dimensional array along first and second dimensions
ans =

8 3 5 7
8 9 1 4
4 4 2 5

> A(:, 2:3, 3) # 3x2 two-dimensional array along first and second dimensions
ans =

3 5
9 1
4 2

> A(1, :, 3) # single-dimension array along second dimension
ans =

8 3 5 7

> A(1, 2, 3) # single value
ans = 3

1976: S
S programming language
S is a statistical programming language developed primarily by John Chambers and Rick Becker and Allan Wilks of Bell Laboratories...

/R

Arrays in S
S programming language
S is a statistical programming language developed primarily by John Chambers and Rick Becker and Allan Wilks of Bell Laboratories...

 and GNU R are always one-based, thus the indices of a new slice will begin with one for each dimension, regardless of the previous indices. Dimensions with length of one will be dropped (unless drop = FALSE). Dimension names (where present) will be preserved.

> A <- array(1:60, dim = c(3, 4, 5)) # 3x4x5 three-dimensional or cubic array
> A[, , 3] # 3x4 two-dimensional array along first and second dimensions
[, 1] [, 2] [, 3] [, 4]
[1,] 25 28 31 34
[2,] 26 29 32 35
[3,] 27 30 33 36
> A[, 2:3, 3, drop = FALSE] # 3x2x1 cubic array subset (preserved dimensions)
, , 1

[, 1] [, 2]
[1,] 28 31
[2,] 29 32
[3,] 30 33
> A[, 2, 3] # single-dimension array along first dimension
[1] 28 29 30
> A[1, 2, 3] # single value
[1] 28

1977: Fortran 77

The Fortran 77 standard introduced the ability to slice and concatenate
Concatenation
In computer programming, string concatenation is the operation of joining two character strings end-to-end. For example, the strings "snow" and "ball" may be concatenated to give "snowball"...

 strings:


PROGRAM MAIN
PRINT *, 'ABCDE'(2:4)
END


Produces:
BCD

Such strings could be passed by reference to another subroutine, the length would also be passed transparently to the subroutine as a kind of short dope vector.


SUBROUTINE PRINT S(STR)
CHARACTER *(*)STR
PRINT *, STR
END

PROGRAM MAIN
CALL PRINT S('ABCDE'(2:4))
END


Again produces:
BCD

1983: Ada 83
Ada (programming language)
Ada is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages...

 and above

Ada 83 supports slices for all array types. Like Fortran 77 such a arrays could be passed by reference to another subroutine, the length would also be passed transparently to the subroutine as a kind of short dope vector.


with Text_IO;

procedure Main is
Text : String := "ABCDE";
begin
Text_IO.Put_Line (Text (2 .. 4));
end Main;


Produces:
BCD

Note: Since in Ada indices are n-based the term Text (2 .. 4) will result in an Array with the base index of 2.

The definition for Text_IO.Put_Line is:


package Ada.Text_IO is

procedure Put_Line(Item : in String);


The definition for String is:


package Standard is

subtype Positive is Integer range 1 .. Integer'Last;

type String is array(Positive range <>) of Character;
pragma Pack(String);


As Ada supports true negative indices as in type History_Data_Array is array (-6000 .. 2010) of History_Data; it places no special meaning on negative indices. In the example above the term Some_History_Data (-30 .. 30) would slice the History_Data from 30 BC to 30 AD
Anno Domini
and Before Christ are designations used to label or number years used with the Julian and Gregorian calendars....

.

1987: Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

If we have
@a = (2, 5, 7, 3, 8, 6, 4)
as above, then the first 3 elements, middle 3 elements and last 3 elements would be:

@a[0..2]; # (2, 5, 7)
@a[3..5]; # (7, 3, 8); #should be (3,8,6) perl -e '@a = (2, 5, 7, 3, 8, 6, 4); print @a[3..5];'
@a[-3..-1]; # (8, 6, 4);

Perl supports negative list indices. The -1 index is the last element, -2 the penultimate element, etc.
In addition Perl supports slicing based on expressions, for example:

@a[ 3.. $#a ]; # 4th element until the end (3, 8, 6, 4)
@a[ grep { !($_ % 3) } (0...$#a) ] # 1st, 4th and 7th element (2,3,4)
@a[ grep { !(($_+1) % 3) } (0..$#a) ] # every 3rd element (7,6)

1991: Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

If you have a list

nums = [1, 3, 5, 7, 8, 13, 20]

, then it is possible to slice by using a notation similar to element retrieval:

nums[3] #equals 7, no slicing
nums[:3] #equals [1, 3, 5], from index 0 (inclusive) until index 3 (exclusive)
nums[1:5] #equals [3, 5, 7, 8]
nums[-3:] #equals [8, 13, 20]

Note that Python allows negative list indices. The index -1 represents the last element, -2 the penultimate element, etc.
Python also allows a step property by append an extra colon and a value. For example:

nums[3::] #equals [7, 8, 13, 20], same as nums[3:]
nums[::3] #equals [1, 7, 20] (starting at index 0 and getting every third element)
nums[1:5:2] #equals [3, 7] (from index 1 until index 5 and getting every second element)

1992: Fortran 90 and above

In Fortran 90, slices are specified in the form


lower_bound:upper_bound[:stride]


Both bounds are inclusive and can be omitted, in which case they default to the declared
array bounds. Stride defaults to 1. Example:


real, dimension(m, n):: a ! declaration of a matrix

print *, a(:, 2) ! second column
print *, a(m, :) ! last row
print *, a(:10, :10) ! leading 10-by-10 submatrix

1998: S-Lang

Array slicing was introduced in version 1.0. Earlier versions did not
support this feature.

Suppose that A is a 1-d array such as

A = [1:50]; % A = [1, 2, 3, ...49, 50]

Then an array B of first 5 elements of A may be created using

B = A:4;

Similarly, B may be assigned to an array of the last 5 elements of A via:

B = A-5:;

Other examples of 1-d slicing include:

A[-1] % The last element of A
A[*] % All elements of A
A::2  % All even elements of A
A1::2  % All odd elements of A
A-1::-2  % All even elements in the reversed order
A[0:3], [10:14] % Elements 0-3 and 10-14


Slicing of higher dimensional arrays works similarly:

A[-1, *] % The last row of A
A1:5], [2:7  % 2d array using rows 1-5 and columns 2-7
A5:1:-1], [2:7  % Same as above except the rows are reversed


Array indices can also be arrays of integers. For example, suppose
that I = [0:9] is an array of 10 integers. Then
A[I] is equivalent to an array of the first 10 elements
of A. A practical example of this is a sorting
operation such as:

I = array_sort(A); % Obtain a list of sort indices
B = A[I]; % B is the sorted version of A
C = A[array_sort(A)]; % Same as above but more concise.

1999: D

Consider the statically allocated array:


static int[] a = [2, 5, 7, 3, 8, 6, 4, 1];


Take a slice out of it:


int[] b = a[2 .. 5];


and the contents of b[] will be [7, 3, 8]. The first index of the slice is inclusive, the second is exclusive. D array slices are aliased to the original array, so:


b[2] = 10;


means that a[] now has the contents [2, 5, 7, 3, 10, 6, 4, 1]. To create a copy of the array data, instead of only an alias, do:


b = a[2..5].dup;

2004: SuperCollider
Supercollider
A Supercollider is a high energy particle accelerator. The term may refer to:* Superconducting Super Collider, planned 80 km project in Texas, canceled in 1993...

The programming language SuperCollider
Supercollider
A Supercollider is a high energy particle accelerator. The term may refer to:* Superconducting Super Collider, planned 80 km project in Texas, canceled in 1993...

 implements some concepts from J
J (programming language)
The J programming language, developed in the early 1990s by Kenneth E. Iverson and Roger Hui, is a synthesis of APL and the FP and FL function-level languages created by John Backus....

/APL. Slicing looks as follows:


a = [3, 1, 5, 7] // assign an array to the variable a
a[0..1] // return the first two elements of a
a[..1] // return the first two elements of a: the zero can be omitted
a[2..] // return the element 3 till last one
a0, 3  // return the first and the fourth element of a

a0, 3 = [100, 200] // replace the first and the fourth element of a
a[2..] = [100, 200] // replace the two last elements of a

// assign a multidimensional array to the variable a
a = 0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19;
a.slice(2, 3); // take a slice with coordinates 2 and 3 (returns 13)
a.slice(nil, 3); // take an orthogonal slice (returns [3, 8, 13, 18])

2005: fish
Friendly interactive shell
The friendly interactive shell is a Unix shell that focuses on interactive use, discoverability, and user friendliness. The design goal of fish is to give the user a rich set of powerful features in a way that is easy to discover, remember, and use.Released in 2005 under the terms of the GNU...

Arrays in fish
Friendly interactive shell
The friendly interactive shell is a Unix shell that focuses on interactive use, discoverability, and user friendliness. The design goal of fish is to give the user a rich set of powerful features in a way that is easy to discover, remember, and use.Released in 2005 under the terms of the GNU...

 are always one-based, thus the indices of a new slice will begin with one, regardless of the previous indices.

> set A (seq 3 2 11) # $A is an array with the values 3, 5, 7, 9, 11

> echo $A[(seq 2)] # Print the first two elements of $A
3 5

> set B $A[1 2] # $B contains the first and second element of $A, i.e. 3, 5

> set -e A[$B]; echo $A # Erase the third and fifth elements of $A, print $A
3 5 9

2006: Cobra
Cobra (programming language from Cobra Language LLC)
Cobra is an object-oriented programming language produced by Cobra Language LLC. Cobra is designed by Chuck Esterbrook, and runs on the Microsoft .NET and Mono platforms. It is strongly influenced by Python, C#, Eiffel, Objective-C, and other programming languages. It supports both static and...

Cobra supports Python-style slicing. If you have a list

nums = [1, 3, 5, 7, 8, 13, 20]

, then the first 3 elements, middle 3 elements, and last 3 elements would be:

nums[:3] # equals [1, 3, 5]
nums[2:5] # equals [5, 7, 8]
nums[-3:] # equals [8, 13, 20]


Cobra also supports slicing-style syntax for 'numeric for loops':

for i in 2 : 5
print i
  1. prints 2, 3, 4


for j in 3
print j
  1. prints 0, 1, 2


2006: Windows PowerShell
Windows PowerShell
Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework...

Arrays are zero-based in PowerShell and can be defined using the comma operator:
PS> $a = 2, 5, 7, 3, 8, 6, 4, 1

Print the first two elements of $a:
PS> $a[0, 1] # Print 2 and 5

Take a slice out of it using the range operator:
PS> $b = $a[2..5] # Content of $b will be: 7, 3, 8, 6.

Get the last 3 elements:
PS> $a[-3..-1] # Print 6, 4, 1

Return the content of the array in reverse order:
PS> $a[($a.Length - 1)..0] # Length is a property of System.Object[]

2009: Go
Go (programming language)
Go is a compiled, garbage-collected, concurrent programming language developed by Google Inc.The initial design of Go was started in September 2007 by Robert Griesemer, Rob Pike, and Ken Thompson. Go was officially announced in November 2009. In May 2010, Rob Pike publicly stated that Go was being...

Go supports Python-style syntax for slicing (except negative indices are not supported). Arrays and slices can be sliced. If you have a slice

nums := []int{1, 3, 5, 7, 8, 13, 20}

then the first 3 elements, middle 3 elements, last 3 elements, and a copy of the entire slice would be:

nums[:3] // equals []int{1, 3, 5}
nums[2:5] // equals []int{5, 7, 8}
nums[4:] // equals []int{8, 13, 20}
nums[:] // equals []int{1, 3, 5, 7, 8, 13, 20}


Slices in Go are reference types, which means that different slices may refer to the same underlying array.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK