# Select

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βπ©`.

## Single selection

The major cells of an array are arranged along the first axis and numbered starting at 0. For a natural number `π¨`, Select returns the major cell of `π©` with index `π¨`.

βοΈ
```    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, the result has rank one less than `π©` and shape `1ββ’π©`. Of course `π©` must have at least one axis to select along, or there are no major cells and you'll get an error.

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β‘β π©)
```

### First Cell

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
```

## First-axis selection

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 the length of `π¨`.

βοΈ
```    2βΏ3βΏ3βΏ0βΏ4βΏ1 β "OlZEt"
"ZEEOtl"

```

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"
β
```

## Multi-axis selection

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
ββ
β
```

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 Rank modifiers `Λ` or `β`, to select on any contiguous set of axes. In particular, use the one-axis case `π¨βΈββ(-k) π©` to select along axis `k` of `π©`.