[] (subscript brackets)
[]
are used to access subscripted values.
This article describes this function, but []
can also be used as ordinary parentheses and to denote a closed interval
of real numbers.
Syntax
-
A[x, ...]
-
x
, ... are indices
-
The returned value is an lvalue if A
is.
Description
[]
are used to access subscripted values.
For linear containers, such as vectors, matrices (considered as linear containers in row-major order), strings, lists, structures, and binary data objects, A[n]
returns the n
th value (number, character, object, value, byte, etc) of A
; it can also be used as an lvalue. Typically, negative indices count from the end of A
: while A[1]
is the first element in A
, A[−1]
is the last element.
For planar containers, such as matrices, pixmaps, and tables, A[i, j]
returns the index on row i
and column j
.
For structures, it is possible to access values both by position and by name: A[1]
or A["FirstName"]
. In the latter case, however, it is more idiomatic to use the member access operator: A.FirstName
.
Notes
When []
are used to access subscripted values, they are implemented by the subscript
function.
In addition to being used to access subscripted values, []
can also be used as ordinary parentheses, just like ()
.
Finally, [a, b]
can also be used to denote a closed interval
of real numbers.
Examples
v ≔ ❨1, 2, 3❩; s ≔ "ball of light";
A ≔ RandomIntMatrix(3, 0, 10)
⎛8 9 4⎞ ⎜3 5 3⎟ ⎝4 5 6⎠
'(v[1], s[−1], A[3], A[2, 3])
1 t 4 3
v[2] ≔ 3; v
⎛1⎞ e⎜3⎟ ⎝3⎠
D ≔ date()
year: 2020 month: September day: 29
'(D[1], D["month"], D[−1], D.day)
2020 September 29 29