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). InHistory
The concept of slicing was surely known even before the invention ofTimeline of slicing in various programming languages
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 a2.000000 4.000000 8.000000Note that there is no
SUBROUTINE
. 1970s Pascal and C had similar restrictions.
1968:
1970s:
1977: Fortran 77
The Fortran 77 standard introduced the ability to slice and1979:
1983: Ada 83 and above
Ada 83 supports slices for all array types. Like Fortran 77 such 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.Text (2 .. 4)
will result in an Array with the base index of 2.
The definition for Text_IO.Put_Line
is:
String
is:
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 31 BC to 30 AD (since there was no year zero, the year number 0 actually refers to 1 BC).
1987:
1991: Python
If you have the following list:nums :5:2/code>) was introduced in the second half of the 1990s, as a result of requests put forward by scientific users in the Python "matrix-SIG" (special interest group).
Slice semantics potentially differ per object; new semantics can be introduced when operator overloading
In computer programming, operator overloading, sometimes termed ''operator ad hoc polymorphism'', is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading i ...
the indexing operator. With Python standard lists (which are dynamic array
In computer science, a dynamic array, growable array, resizable array, dynamic table, mutable array, or array list is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard lib ...
s), every slice is a copy. Slices of NumPy arrays, by contrast, are views onto the same underlying buffer.
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
1994: Analytica
Each dimension of an array value in Analytica is identified by an Index variable. When slicing or subscripting, the syntax identifies the dimension(s) over which you are slicing or subscripting by naming the dimension. Such as:
Index I := 1..5
Index J := A', 'B', 'C'
Variable X := Array(I, J, 10, 20, 30
1 (one, unit, unity) is a number representing a single or the only entity. 1 is also a numerical digit and represents a single unit of counting or measurement. For example, a line segment of ''unit length'' is a line segment of length 1. I ...
, 2, 3
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
....])
X = 1, J = 'B' -> 20
X = 1-> Array(J, 0, 20, 30
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
X = 2-> Array(I, 0, 2, ....
X = 1..3
Naming indexes in slicing and subscripting is similar to naming parameters in function calls instead of relying on a fixed sequence of parameters. One advantage of naming indexes in slicing is that the programmer does not have to remember the sequence of Indexes, in a multidimensional array. A deeper advantage is that expressions generalize automatically and safely without requiring a rewrite when the number of dimensions of X changes.
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 = :50 % A = , 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
A 1::2 % All odd elements of A
A -1::-2 % All even elements in the reversed order
A [0:3 [10:14">[0:3.html" ;"title="[0:3">[0:3 [10:14">:3.html"_;"title="[0:3">[0:3<_a>_[10:14.html" ;"title="[0:3.html" ;"title="[0:3">[0:3 [10:14">[0:3.html" ;"title="[0:3">[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 = :9/code> is an array of 10 integers. Then
A /code> 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 % B is the sorted version of A
C = A rray_sort(A) % Same as above but more concise.
1999: D
Consider the array:
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.
auto c = a - 4 .. $ - 2
means that the dynamic array c
now contains , 6
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline (t ...
/code> because inside the [] the $
symbol refers to the length of the array.
D array slices are aliased to the original array, so:
b[2] = 10;
means that a
now has the contents , 5, 7, 3, 10, 6, 4, 1
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
/code>. To create a copy of the array data, instead of only an alias, do:
auto b = a .. 5dup;
Unlike Python, D slice bounds don't saturate, so code equivalent to this Python code is an error in D:
>>> d = 0, 20, 30
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
>>> d : 50, 30
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
2004:
SuperCollider
A particle accelerator is a machine that uses electromagnetic fields to propel charged particles to very high speeds and energies, and to contain them in well-defined beams.
Large accelerators are used for fundamental research in particle ...
The programming language SuperCollider
A particle accelerator is a machine that uses electromagnetic fields to propel charged particles to very high speeds and energies, and to contain them in well-defined beams.
Large accelerators are used for fundamental research in particle ...
implements some concepts from J/ APL. Slicing looks as follows:
a = , 1, 5, 7
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
// assign an array to the variable a
a ..1 // return the first two elements of a
a .1 // return the first two elements of a: the zero can be omitted
a .. // return the element 3 till last one
a0, 3
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
// return the first and the fourth element of a
a0, 3
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
= 00, 200
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
// replace the first and the fourth element of a
a ..= 00, 200
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
// replace the two last elements of a
// assign a multidimensional array to the variable a
a = 0, 1, 2, 3, 4
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
, 6, 7, 8, 9
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
0, 11, 12, 13, 14
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
5, 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 , 8, 13, 18
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
2005:
fish
Fish are aquatic, craniate, gill-bearing animals that lack limbs with digits. Included in this definition are the living hagfish, lampreys, and cartilaginous and bony fish as well as various extinct related groups. Approximately 95% ...
Arrays in fish
Fish are aquatic, craniate, gill-bearing animals that lack limbs with digits. Included in this definition are the living hagfish, lampreys, and cartilaginous and bony fish as well as various extinct related groups. Approximately 95% ...
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 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 supports Python-style slicing. If you have a list
nums = , 3, 5, 7, 8, 13, 20
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
then the first 3 elements, middle 3 elements, and last 3 elements would be:
nums 3 # equals , 3, 5nums :5 # equals , 7, 8
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
nums 3: # equals , 13, 20
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline o ...
Cobra also supports slicing-style syntax for 'numeric for loops':
for i in 2 : 5
print i
# prints 2, 3, 4
for j in 3
print j
# prints 0, 1, 2
2006:
Windows PowerShell
PowerShell is a task automation and configuration management program from Microsoft, consisting of a command-line shell and the associated scripting language. Initially a Windows component only, known as Windows PowerShell, it was made open-s ...
Arrays are zero-based in PowerShell and can be defined using the comma operator:
PS > $a = 2, 5, 7, 3, 8, 6, 4, 1
PS > # Print the first two elements of $a:
PS > "$($a , 1"
2 5
PS > # Take a slice out of it using the range operator:
PS > "$($a ..5"
7 3 8 6
PS > # Get the last 3 elements:
PS > "$($a 3..-1"
6 4 1
PS > # Return the content of the array in reverse order:
PS > "$($a $a.Length - 1)..0" # Length is a property of System.Object[]
1 4 6 8 3 7 5 2
2009: Go
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
then the first 3 elements, middle 3 elements, last 3 elements, and a copy of the entire slice would be:
nums 3 // equals []int
nums :5// equals []int
nums[4:] // equals []int
nums[:] // equals []int
Slices in Go are reference types, which means that different slices may refer to the same underlying array.
2010: Cilk Plus
Cilk Plus supports syntax for array slicing as an extension to C and C++.
array_base ower_bound:length[:stride*
Cilk Plus slicing looks as follows:
A[:">stride.html" ;"title="ower_bound:length[:stride">ower_bound:length[:stride*
Cilk Plus slicing looks as follows:
A[: // All of vector A
B[2:6] // Elements 2 to 7 of vector B
C[:][5] // Column 5 of matrix C
D[0:3:2] // Elements 0, 2, 4 of vector D
Cilk Plus's array slicing differs from Fortran's in two ways:
* the second parameter is the length (number of elements in the slice) instead of the upper bound, in order to be consistent with standard C libraries;
* slicing never produces a temporary, and thus never needs to allocate memory. Assignments are required to be either non-overlapping or perfectly overlapping, otherwise the result is undefined.
2012:
Julia
Julia is usually a feminine given name. It is a Latinate feminine form of the name Julio and Julius. (For further details on etymology, see the Wiktionary entry "Julius".) The given name ''Julia'' had been in use throughout Late Antiquity (e ...
Julia array slicing
is like that of MATLAB
MATLAB (an abbreviation of "MATrix LABoratory") is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementa ...
, but uses square brackets. Example:
julia> x = rand(4, 3)
4x3 Array:
0.323877 0.186253 0.600605
0.404664 0.894781 0.0955007
0.223562 0.18859 0.120011
0.149316 0.779823 0.0690126
julia> x , 2 # get the second column.
4-element Array:
0.186253
0.894781
0.18859
0.779823
julia> x , : # get the first row.
1x3 Array:
0.323877 0.186253 0.600605
julia> x :2,2:3 # get the submatrix spanning rows 1,2 and columns 2,3
2x2 Array:
0.186253 0.600605
0.894781 0.0955007
See also
*
References
{{DEFAULTSORT:Array Slicing
Arrays
Programming constructs
Articles with example Ada code
Articles with example ALGOL 68 code
Articles with example D code
Articles with example Fortran code
Articles with example Perl code
Articles with example Python (programming language) code