indexing operator - Maple Help

?[]

the indexing operator

 Calling Sequence ?[](x, l) ?[](x, l, m)

Parameters

 x - expression to be indexed l - list of indices m - list of values to assign

Description

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

Examples

 > $\mathrm{tbl}≔\mathrm{table}\left(\left[\mathrm{one}=1,\mathrm{two}=2,\mathrm{three}=3,\mathrm{four}=4\right]\right)$
 ${\mathrm{tbl}}{≔}{table}{}\left(\left[{\mathrm{one}}{=}{1}{,}{\mathrm{two}}{=}{2}{,}{\mathrm{three}}{=}{3}{,}{\mathrm{four}}{=}{4}\right]\right)$ (1)

To obtain a table entry, you can index the table directly, or use the indexing operator.

 > $\mathrm{tbl}\left[\mathrm{one}\right]=\mathrm{?\left[\right]}\left(\mathrm{tbl},\left[\mathrm{one}\right]\right)$
 ${1}{=}{1}$ (2)

To assign a table entry, you can use indexing syntax, or use the indexing operator.

 > $\mathrm{tbl}\left[\mathrm{five}\right]≔5$
 ${{\mathrm{tbl}}}_{{\mathrm{five}}}{≔}{5}$ (3)
 > $\mathrm{?\left[\right]}\left(\mathrm{tbl},\left[\mathrm{six}\right],\left[6\right]\right)$
 ${{\mathrm{tbl}}}_{{\mathrm{six}}}$ (4)
 > $\mathrm{eval}\left(\mathrm{tbl}\right)$
 ${table}{}\left(\left[{\mathrm{one}}{=}{1}{,}{\mathrm{two}}{=}{2}{,}{\mathrm{five}}{=}{5}{,}{\mathrm{six}}{=}{6}{,}{\mathrm{three}}{=}{3}{,}{\mathrm{four}}{=}{4}\right]\right)$ (5)

You can use the indexing operator to obtain a list of entries in a table, using the map2 command.

 > $\mathrm{ids}≔\left[\left[\mathrm{two}\right],\left[\mathrm{six}\right],\left[\mathrm{three}\right],\left[\mathrm{two}\right]\right]$
 ${\mathrm{ids}}{≔}\left[\left[{\mathrm{two}}\right]{,}\left[{\mathrm{six}}\right]{,}\left[{\mathrm{three}}\right]{,}\left[{\mathrm{two}}\right]\right]$ (6)
 > $\mathrm{map2}\left(\mathrm{?\left[\right]},\mathrm{tbl},\mathrm{ids}\right)$
 $\left[{2}{,}{6}{,}{3}{,}{2}\right]$ (7)

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.

 > $\mathrm{lst}≔\left[4,5,6\right]$
 ${\mathrm{lst}}{≔}\left[{4}{,}{5}{,}{6}\right]$ (8)
 > $\mathrm{vec}≔⟨7,8,9⟩$
 ${\mathrm{vec}}{≔}\left[\begin{array}{c}{7}\\ {8}\\ {9}\end{array}\right]$ (9)
 > $\mathrm{map}\left(\mathrm{?\left[\right]},\left[\mathrm{lst},\mathrm{vec},\mathrm{var}\right],\left[2\right]\right)$
 $\left[{5}{,}{8}{,}{{\mathrm{var}}}_{{2}}\right]$ (10)