The function Select (`β`

) reorganizes the array `π©`

along one or more axes based on indices given by `π¨`

. The result has the same depth as `π©`

, since its elements are always elements of `π©`

. This means it differs from Pick (`β`

), which takes elements from `π©`

but can arrange them in any nested structure, including returning an element directly.

The monadic form First Cell (`β`

) gets the major cell with index 0, so that `βπ©`

is identical to `0βπ©`

.

Each axis of a BQN array is numbered starting at zero. Major cells are arranged along the first axis; in accordance with the leading axis principle, Select returns a major cell of `π©`

when `π¨`

is an atom.

2 β "abcdef" # An enclosed element βΒ· Β·'c' β 2 β "abcdef" # Pick gets a non-enclosed element 'c' 2 β >"nul"βΏ"one"βΏ"two"βΏ"tre"βΏ"for" "two" 0 β <5 # No first axis to select from Error: β: π© cannot be a unit

As a major cell of `π©`

, the result has rank one less than it and its shape is `1ββ’π©`

. `π©`

must have rank one or more.

The index `π¨`

has to be an integer less than `β π©`

. It can be negative, in which case it must be greater than or equal to `-β π©`

. Negative indices select from the end of `π©`

, in that `Β―1`

indicates the last major cell and `-β π©`

indicates the first. If `β π©`

is 0, then no index is valid.

Β―2 β "abcdef" βΒ· Β·'e' β 0 β "" Error: β: Indexing out-of-bounds (π¨β‘0, 0β‘β π©)

The monadic case First Cell (`βπ©`

) is identical to `0βπ©`

. It has the same restrictions: `π©`

must have rank 1 or more, and length 1 or more.

β "abc" βΒ· Β·'a' β β "abc"β"def" "abc" β β "abc" "abc" β 'a' Error: β: Argument cannot be an atom

If `π¨`

is an array of numbers (including any empty array), then each number indicates a major cell of `π©`

. In the simplest case, a list of numbers gives a result with the same rank as `π©`

but maybe not the same length.

2βΏ3βΏ3βΏ0βΏ4βΏ1 β "OlZEt" "ZEEOtl" β¨β© β "OlZEt" β¨β©

To find the first and last cells of `π©`

, use `0βΏΒ―1`

for the left argument.

β’ m β 3βΏ5βΏ7βΏ11 |β ΓΛβ7 ββ β΅ 0 1 1 0 1 1 0 0 1 4 4 1 0 1 0 1 4 2 2 4 1 0 1 4 9 5 3 3 β 0βΏΒ―1 β m ββ β΅ 0 1 1 0 1 1 0 0 1 4 9 5 3 3 β

More generally, `π¨`

can be an array of any rank. Each of its 0-cellsβcontaining a single numberβis replaced with a cell of `π©`

in the result. The result's shape is then made up of the shape of `π¨`

and the major cell shape of `π©`

: it's `(β’π¨)βΎ1ββ’π©`

.

When `π©`

is a list, the result has the same shape as `π¨`

. Elements of `π¨`

are replaced one-for-one with elements of `π©`

.

2|m ββ β΅ 0 1 1 0 1 1 0 0 1 0 0 1 0 1 0 1 0 0 0 0 1 0 1 0 1 1 1 1 β (2|m) β " *" ββ β΅" ** ** * * * * * * ****" β

Another special case is when `π¨`

is a unit. Now the result shape will be the major cell shape of `π©`

. In fact it's the same as the atom case above, that is, for a number `n`

, `(<n)βπ©`

is the same as `nβπ©`

.

The general case can result in a complicated array. Remember that the initial axes come from `π¨`

while later ones come from `π©`

.

"awA0" +β β4 ββ β΅"abcd wxyz ABCD 0123" β 2 β β4 ββ β΅ 0 1 1 2 2 3 β (2 β β4) β "awA0" +β β4 ββ β"abcd wxyz Β·wxyz ABCD Β·ABCD 0123" β

Select also allows `π¨`

to apply to multiple axes of `π©`

simultaneously. For this case, `π¨`

must be a non-empty list (or unit array) where every element is an array of indices.

β¨2βΏ1, 3βΏ0βΏ0β© β β3βΏ4 ββ β΅ β¨ 2 3 β© β¨ 2 0 β© β¨ 2 0 β© β¨ 1 3 β© β¨ 1 0 β© β¨ 1 0 β© β

Using a range for `π©`

shows the structure of the selected elements more clearly, because each element is its own index. Each element of `π¨`

acts independently, giving a structure like the Table modifier.

While `π¨`

must have rank one or less, its elements can have any rank. When the elements are units, the corresponding axis of `π©`

disappears from the result. We can select a 0-cell of `π©`

in this way, although the more common case of selecting an element is handled by Pick.

β¨<4,<5,<1β© β (3β₯10)β₯β1e3 βΒ· Β· 451 β β¨ 4, 5, 1β© β (3β₯10)β₯β1e3 451

However, the `<Β¨βΈβ`

construct can select a cell of any rank from `π©`

, because `β π¨`

can be smaller than `=π©`

(okay, not quite: an empty list is always interpreted as a list of indices, so it's impossible to select the full-rank cell `π©`

). Below, `π¨`

is missing one axis and the result is a 1-cell, or row, of `π©`

.

β¨4,5β© <Β¨βΈβ (3β₯10)β₯β1e3 β¨ 450 451 452 453 454 455 456 457 458 459 β©

If an element of `π¨`

has rank more than 1, it increases the rank of `π©`

rather than decreasing it. The general rule is that in the result, one axis of `π©`

is replaced by all the axes of the corresponding element of `π¨`

(trailing axes are unchanged). So the final shape `β’π¨βπ©`

is `(βΎβ’Β¨π¨)βΎπ¨β βΈββ’π©`

. But this shape doesn't affect the elements retrieved from `π©`

. In all cases, using `β₯Β¨π¨`

for the left argument and then reshaping the result would yield the same value.

Selection only ever applies to leading axes of `π©`

. However, you can skip some leading axes using `Λ`

or `β`

, to select on any contiguous set of axes. In particular, use the one-axis case `π¨βΈββ(-k) π©`

to select along axis `k`

of `π©`

.