`?[]`
the indexing operator
Calling Sequence
Parameters
Description
Examples
`?[]`(x, l)
`?[]`(x, l, m)
x
-
expression to be indexed
l
list of indices
m
list of values to assign
The indexing operator, `?[]`, provides a way to encode the process of indexing an expression in a function call.
The first calling sequence evaluates to the expression x, indexed by the entries of the list l. For example, `?[]`(tbl, [a, b]) evaluates to tbl[a, b].
Evaluating the second calling sequence assigns the expression sequence contained in the list m to the entry that the first calling sequence evaluates to. For example, evaluating `?[]`(tbl, [a, b], [c, d]) is the same as running the statement tbl[a, b] := c, d;.
The help page object,operators discusses some of the uses and consequences of overriding the indexing operator in an object. Briefly, it allows the object author to define the behavior when an object is indexed.
tbl≔table⁡one=1,two=2,three=3,four=4
tbl≔table⁡three=3,four=4,one=1,two=2
To obtain a table entry, you can index the table directly, or use the indexing operator.
tblone=`?[]`⁡tbl,one
1=1
To assign a table entry, you can use indexing syntax, or use the indexing operator.
tblfive≔5
`?[]`⁡tbl,six,6
tblsix
eval⁡tbl
table⁡six=6,five=5,three=3,four=4,one=1,two=2
You can use the indexing operator to obtain a list of entries in a table, using the map2 command.
ids≔two,six,three,two
map2⁡`?[]`,tbl,ids
2,6,3,2
The indexing operator works with all expressions that can be indexed, not just tables. The following map statement indexes a list, a Vector, and a variable with the number 2. This returns the second entries of the list and the Vector, and the unevaluated indexed variable.
lst≔4,5,6
vec≔7,8,9
vec≔789
map⁡`?[]`,lst,vec,var,2
5,8,var2
See Also
selection
Download Help Document