---
title: Searching and indexing
description: Find values and patterns, remove repetitions, select items and order
  them
skip_showdoc: true
---
Read [Nested arrays and trains](nested-arrays.md) first.

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Membership and repeated values

### [`∊`](https://bpl.fast.ai//glyphs/epsilon.html) (Epsilon)

#### monadic [`∊`](https://bpl.fast.ai//glyphs/epsilon.html) (Enlist)

Enlist recursively collects all the atoms in nested arrays into one vector.

```bpl
mat←[0 1 2◇3 4 5]
mat
```

```output
0 1 2
3 4 5
```

```bpl
∊[0 mat [7 8] "nine"]
```

```output
0 0 1 2 3 4 5 7 8 'n' 'i' 'n' 'e'
```

#### dyadic [`∊`](https://bpl.fast.ai//glyphs/epsilon.html) (Member of)

Member of tests whether each item of its left argument is an item of the vector on its right, comparing whole items. Here neither `"ab"` nor `"abcd"` matches the string `"abc"`.

```bpl
["abc" 4]∊[4 "ab" "abcd"]
```

```output
$f $t
```

```bpl
mat∊6 2 7 4
```

```output
$f $f $t
$f $t $f
```

When the right argument is a vector, Member of tests each item of the left argument. When the right argument is a matrix, it tests each row of the left argument:

```bpl
[3 4 5◇6 7 8]∊mat
```

```output
$t $f
```

### [`⍷`](https://bpl.fast.ai//glyphs/epsilon-underbar.html) (Epsilon Underbar)

#### dyadic [`⍷`](https://bpl.fast.ai//glyphs/epsilon-underbar.html) (Find)

Find marks the start of each occurrence of a pattern. Occurrences can overlap, as in `"ana"` within `"Banana"`.

```bpl
1⍷3 1 4 1 5 9 2
```

```output
$f $t $f $t $f $f $f
```

```bpl
1 4⍷3 1 4 1 5 9 2
```

```output
$f $t $f $f $f $f $f
```

```bpl
"ana"⍷"Banana"
```

```output
$f $t $f $t $f $f
```

A pattern can have more than one dimension:

```bpl
pattern←[0 1◇1 0]
board←[0 1 0 0◇1 0 0 1◇0 0 1 0◇0 1 0 0]
[pattern board]
```

```output
[[0 1◇1 0] [0 1 0 0◇1 0 0 1◇0 0 1 0◇0 1 0 0]]
```

```bpl
pattern⍷board
```

```output
$t $f $f $f
$f $f $t $f
$f $t $f $f
$f $f $f $f
```

#### monadic [`⍷`](https://bpl.fast.ai//glyphs/epsilon-underbar.html) (Groups)

Groups gives the positions of each distinct major cell, in order of first appearance:

```bpl
⍷"Banana"
```

```output
(0◇1 3 5◇2 4)ₓ
```

The length of each group is the number of occurrences of its major cell:

```bpl
≢¨⍷"Banana"
```

```output
[1 3 2]ₓ
```

### [`∩`](https://bpl.fast.ai//glyphs/up-shoe.html) (Up shoe)

#### monadic [`∩`](https://bpl.fast.ai//glyphs/up-shoe.html) (Duplicates)

Duplicates keeps one copy of each major cell that occurs more than once:

```bpl
∩"mississippi"
```

```output
"isp"
```

#### dyadic [`∩`](https://bpl.fast.ai//glyphs/up-shoe.html) (Intersection)

Intersection keeps the major cells of the left argument found in the right argument. It preserves their order and repetitions.

```bpl
"ABRA"∩"CAR"
```

```output
"ARA"
```

```bpl
[22 "ab" "fg"]∩['a' "ab" 22]
```

```output
22 "ab"
```

### [`∪`](https://bpl.fast.ai//glyphs/down-shoe.html) (Down shoe)

#### monadic [`∪`](https://bpl.fast.ai//glyphs/down-shoe.html) (Unique)

Unique keeps the first occurrence of each distinct major cell:

```bpl
a←2 3 4 2 3 4 5
```

```bpl
∪a
```

```output
2 3 4 5
```

Unique mask followed by Replicate gives the same result:

```bpl
≠a # a
```

```output
2 3 4 5
```

```bpl
∪["ab" "ba" "ab" 1 1 2]
```

```output
"ab" "ba" 1 2
```

#### dyadic [`∪`](https://bpl.fast.ai//glyphs/down-shoe.html) (Union)

Union appends the major cells of the right argument that are absent from the left. Repetitions already present in either argument stay.

```bpl
"ABRA"∪"CAR"
```

```output
"ABRAC"
```

```bpl
[22 "ab" "fg"]∪['a' "ab" 22]
```

```output
22 "ab" "fg" 'a'
```

## Sort / index

### [`⌷`](https://bpl.fast.ai//glyphs/squad.html) (Squad)

#### monadic [`⌷`](https://bpl.fast.ai//glyphs/squad.html) (Materialise)

`⌷Y` returns `Y` unchanged.

Its inverse also leaves the argument unchanged:

```bpl
⌷⁻1 2 3
```

```output
1 2 3
```

#### dyadic [`⌷`](https://bpl.fast.ai//glyphs/squad.html) (Index)

Indexing reads items at chosen positions. Those items form a selection. [`⌷`](https://bpl.fast.ai//glyphs/squad.html) uses each item of its left argument to select along one axis, starting with axis 0. A vector of positions for a single axis must be one item of the left argument, hence the nested brackets in `[[3 5]]`.

```bpl
v←2×ι12
3⌷v
```

```output
6
```

```bpl
[[3 5]]⌷v
```

```output
6 10
```

```bpl
mat←[0 1 2 3◇4 5 6 7◇8 9 10 11]
mat
```

```output
0 1  2  3
4 5  6  7
8 9 10 11
```

```bpl
2 3⌷mat
```

```output
11
```

With two vectors, [`⌷`](https://bpl.fast.ai//glyphs/squad.html) takes every combination of the selected rows and columns:

```bpl
[0 2;1 3]⌷mat
```

```output
1  3
9 11
```

### Bracket indexing

A dot before the brackets makes a selection. Positions count from 0; negative positions count from the end:

```bpl
v.[3]
```

```output
6
```

The outer brackets contain one index per axis. To select several positions along one axis, put a vector inside them:

```bpl
v.[[3 5]]
```

```output
6 10
```

`.[]` pairs its index arrays, rather than taking every combination. With the same row and column positions, `.[]` now reads two items:

```bpl
mat.[[0 2] [1 3]]
```

```output
1 11
```

#### Digit subscripts

A digit subscript selects one major cell along axis 0. A negative subscript counts from the end. Touching digits form one position: `₁₂` selects position 12, and `₋₁₂` counts twelve places from the end. On a matrix, one subscript selects a row. Spaced subscripts form a coordinate: `mat₁ ₂` selects row 1, column 2. Because a dot followed by a digit is a decimal point, write `v₂` or `v.[2]` rather than `v.2` for position 2:

```bpl
v←10 20 30 40 50
[v₁ v₋₁]
```

```output
20 50
```

```bpl
mat₁
mat₁ ₂
```

```output
4 5 6 7
6
```

A subscript attaches to the preceding item before surrounding functions apply. `+/mat₁` selects row 1 before adding its items. In `v₁²`, item 1 is selected and then squared:

```bpl
+/mat₁
```

```output
22
```

```bpl
v₁²
```

```output
400
```

After a space, the subscript or coordinate selects from the whole run on its left. A spaced subscript needs a space after it too:

```bpl
10 20 30 ₁
```

```output
20
```

```bpl
,mat ₁
```

```output
1
```

A digit subscript needs an axis to select along. On an atom or scalar, which has none, it raises an `INDEX` error.

#### Whole axes and new axes

Axes omitted at the end stay whole. Selecting a single position removes its axis, while selecting an array of positions replaces that axis with the index array's shape. Paired index arrays occupy the first index array's place in the result, and whole axes keep their places:

```bpl
mat₁
```

```output
4 5 6 7
```

```bpl
ρmat.[[[0 1]◇[1 2]]]
```

```output
[2 2 4]ₓ
```

[`:`](https://bpl.fast.ai//glyphs/colon.html) takes one whole axis. Here the rows stay whole and columns 1 and 3 are selected:

```bpl
mat.[: [1 3]]
```

```output
1  3
5  7
9 11
```

Keep [`:`](https://bpl.fast.ai//glyphs/colon.html) as a separate item, with a space after it, as in `mat.[: 1]`. In `mat.[:1]`, [`:`](https://bpl.fast.ai//glyphs/colon.html) is a function applied to `1`, and the selection is row 1.

[`∞`](https://bpl.fast.ai//glyphs/infinity.html) takes all remaining axes at that position. At most one [`∞`](https://bpl.fast.ai//glyphs/infinity.html) is allowed. Here it keeps both axes, so the result is `mat`:

```bpl
mat.[∞]
```

```output
0 1  2  3
4 5  6  7
8 9 10 11
```

A selector after [`∞`](https://bpl.fast.ai//glyphs/infinity.html) applies to the last axis. Here [`∞`](https://bpl.fast.ai//glyphs/infinity.html) keeps the rows and `1` selects column 1:

```bpl
mat.[∞ 1]
```

```output
1 5 9
```

An empty bracket list selects the whole array unchanged:

```bpl
mat.[]
```

```output
0 1  2  3
4 5  6  7
8 9 10 11
```

`¯∞` inserts a new unnamed axis of length 1, without consuming an input axis. The new axis goes where `¯∞` stands in the brackets:

```bpl
ρmat.[: ¯∞ :]
```

```output
[3 1 4]ₓ
```

```bpl
ρmat.[∞ ¯∞]
```

```output
[3 4 1]ₓ
```

#### Pairing and coordinates

Index arrays agree in shape as [arithmetic arguments](../evaluation.md#agreement-and-pervasion) do. This selects column 2 from row 0, column 0 from row 1 and column 3 from row 2:

```bpl
mat.[ι3ₓ [2 0 3]]
```

```output
2 4 11
```

Repeating the same index array selects a diagonal, with row and column positions equal:

```bpl
i←ι3ₓ
mat.[i i]
```

```output
0 5 10
```

A coordinate lists positions along successive axes, such as `[0 1]` for row 0, column 1. With an array of coordinates, `.[]` reads the item or cell at each coordinate. Where gives coordinates for a condition on a matrix:

```bpl
where←⍸mat>8
where
```

```output
(2 1◇2 2◇2 3)ₓ
```

```bpl
mat.[where]
```

```output
9 10 11
```

A coordinate array takes up one axis for each position in a coordinate. Any further index arrays pair with the coordinates. In this rank-3 array, two coordinates select rows, and the last index gives a column for each row:

```bpl
cube←2 3 4ρι24
coords←[[0 1] [1 2]]
cube.[coords [3 0]]
```

```output
7 20
```

A path can continue into nested items, as Pick does. These paths select item 2 of item 1 and item 0 of item 0:

```bpl
nested←[10 20;30 40 50]
nested.[[[1 2] [0 0]]]
```

```output
50 10
```

The cells selected by the paths must all have the same shape. After paths go into nested items, [`:`](https://bpl.fast.ai//glyphs/colon.html), [`∞`](https://bpl.fast.ai//glyphs/infinity.html) and `¯∞` can keep or add axes of those cells:

```bpl
nested←[[1 2 3◇4 5 6] [7 8 9◇10 11 12]]
paths←[[0 1] [1 0]]
nested.[paths]
```

```output
4 5 6
7 8 9
```

```bpl
ρnested.[paths ∞ ¯∞]
```

```output
[2 3 1]ₓ
```

#### Keys and dotted paths

Use a string to select a key. `.name` reads the key `"name"`, whereas `.[...]` can compute a key from a variable or expression. Dotted paths continue from left to right through nested records:

```bpl
rec←["price":10 "qty":2 "address":["city":"LA"]]
rec
```

```output
["price":10 "qty":2 "address":["city":"LA"]]
```

```bpl
rec.address.city
```

```output
"LA"
```

```bpl
field←"qty"
rec.[field]
```

```output
2
```

```bpl
rec.["pr","ice"]
```

```output
10
```

`T.(expression)` evaluates an expression with `T`'s keys as names. A key's name takes precedence over an outer name with the same spelling. Other names still refer to their surrounding definitions. Assigning to a name inside `T.(...)` does not change `T`:

```bpl
rec.(price×qty)
```

```output
20
```

With keys on a matrix's first axis, each name stands for a whole major cell:

```bpl
table←"left" "right":[1 2 3◇4 5 6]
table.(left+right)
```

```output
5 7 9
```

```bpl
rate←0.5
rec.(price×rate)
```

```output
5
```

```bpl
rec.(price←3)
rec.price
```

```output
3
10
```

A key's value can also be a function. After a function, however, a dot before another function makes an inner product, as in `+.×`.

```bpl
ops←["double":2×]
ops.double 3
```

```output
6
```

Put a space before the argument when calling a function stored in a field, because `ops.double3` reads the key `"double3"`.

#### Function-result selections and binding

After a function, `.name` and `.[...]` select from its result. Same [`⊢`](https://bpl.fast.ai//glyphs/right-tack.html) returns its argument unchanged, so `⊢.address.city` reads that field. A standalone `.[I]` is a selector function. The suffix also works after a dyadic function:

```bpl
⊢.address.city rec
```

```output
"LA"
```

```bpl
.[[1 3]] v
```

```output
20 40
```

```bpl
1 2 3 4 5 +.[[0 2]] v
```

```output
11 33
```

Use Each to read a field from every record in a vector. To evaluate an expression for every record, apply a dfn with Each; `T.(...)` itself binds the keys on `T`'s first axis, rather than mapping over records:

```bpl
orders←[rec ["price":5 "qty":3]]
⊢.price¨orders
```

```output
10 5
```

```bpl
{ω.(price×qty)}¨orders
```

```output
20 15
```

Dotted paths and subscripts bind more tightly than functions. `2×rec.price` doubles the price, and `rec.address.city₁` reads character 1 of the city. A path is read left to right, with the array evaluated before its index:

```bpl
2×rec.price
```

```output
20
```

```bpl
rec.address.city₁
```

```output
'A'
```

#### Named indices

An index array can have a named axis. Indices with the same axis name pair; indices with different names keep separate axes and build every combination:

```bpl
r←ι["r":3] ◇ c←ι["c":4]
mat.[r c]
```

```output
0 1  2  3
4 5  6  7
8 9 10 11
```

If only one index has a given axis name, it selects the data axis with that name, wherever it stands in the brackets. Axis-qualified Iota gives an index with the axis's name and keys:

```bpl
sales←["city":"NY" "LA";"month":"Jan" "Feb" "Mar"]:[10 20 30◇40 50 60]
sales
```

```output
   Jan Feb Mar
NY  10  20  30
LA  40  50  60
```

```bpl
cy←ι⍠"city" sales ◇ mo←ι⍠"month" sales
sales≡sales.[mo cy]
```

```output
$t
```

You can select keys with strings on any axis, and mix strings with positions:

```bpl
sales.[1 "Feb"]
```

```output
50
```

Selecting with a single string removes its axis. Selecting with a one-item vector of strings keeps that axis, with length 1:

```bpl
ρsales.["LA"]
```

```output
["month":3]ₓ
```

```bpl
ρsales.[["LA"]]
```

```output
["city":1 "month":3]ₓ
```

When several selectors share an axis name, they pair and select the data's axes in order instead. Both selectors below have the name `"col"`: one gives a row for each column, and the other gives the column:

```bpl
data←["row":3 "col":4]ρmat
cols←ι["col":4] ◇ rows←["col":4]ρ2 0 1 2
data.[rows cols]
```

```output
["col":4]ρ[8 1 6 11]
```

#### Reading fill and changing selections

Reading at a position outside its axis, or at a position that is not a whole number, returns the fill, which is zero for these numbers. NaN, written `$n`, isn't a whole number. Negative positions still count from the end:

```bpl
v.[[¯1 $n 0]]
```

```output
50 0 10
```

An assignment through a selection changes the original array. Assigning to a single position stores the whole value there, while assigning to a vector selection replaces its selected items. For a subscript target, put no space before the first subscript: write `v₁←5`, not `v ₁←5`:

```bpl
v₁←5
v
```

```output
10 5 30 40 50
```

With `+←`, modified assignment adds to the existing values instead of replacing them. The function is applied once for each appearance of a repeated position. Assigning to a position that isn't a whole number, such as NaN, changes nothing:

```bpl
v.[[1 3 1 $n]]+←1
v
```

```output
10 7 30 41 50
```

```bpl
v₁+←1
v
```

```output
10 8 30 41 50
```

Plain assignment can repeat a position. Which value a repeated position keeps isn't promised, so the two values assigned here are equal:

```bpl
v.[[0 0 $n]]←7 7 99
v
```

```output
7 8 30 41 50
```

[`⌷`](https://bpl.fast.ai//glyphs/squad.html) writes to every combination of its indices instead of pairing them. The space before [`←`](https://bpl.fast.ai//glyphs/left-arrow.html) makes the whole run on its left the target. A dotted target such as `v.[[1 3]]` is one run already, so it needs no space:

```bpl
grid←3 4ρ0
[0 2;1 3]⌷grid +←10
grid
```

```output
0 10 0 10
0  0 0  0
0 10 0 10
```

Plain assignment through a missing key adds it, and creates missing records along a dotted path. Modified assignment adds a missing key too, starting from the fill. Reading a missing key returns the fill, and assigning by position past the end of an axis changes nothing:

```bpl
rec.style.color←"red"
rec.style
```

```output
["color":"red"]
```

```bpl
rec.price+←2
rec.price
```

```output
12
```

```bpl
sales.["SF"]←70 80 90
sales.[: "Apr"]←1 2 3
sales
```

```output
   Jan Feb Mar Apr
NY  10  20  30   1
LA  40  50  60   2
SF  70  80  90   3
```

Adding a row and column together fills the other new cells with zero:

```bpl
sales.["TX" "May"]←9
sales
```

```output
   Jan Feb Mar Apr May
NY  10  20  30   1   0
LA  40  50  60   2   0
SF  70  80  90   3   0
TX   0   0   0   0   9
```

Assigning to a vector selection matches replacement keys to the keys on the axis it keeps. Here the replacement lists the keys in reverse order, but each new price goes to its matching key. Assigning to a single key instead stores the whole right argument as that item's value:

```bpl
prices←["aa":1 "bb":2]
prices.[["aa" "bb"]]←["bb":20 "aa":10]
prices
```

```output
["aa":10 "bb":20]
```

```bpl
prices.["aa"]←[7 8]
prices.aa
```

```output
7 8
```

An assignment can repeat a position or a key. Which replacement it keeps there isn't promised. Selecting the same key more than once drops the selected axis's keys. Assigning through that selection keeps the original array's keys.

### [`⍒`](https://bpl.fast.ai//glyphs/del-stile.html) (Grade Down)

#### monadic [`⍒`](https://bpl.fast.ai//glyphs/del-stile.html) (Grade Down)

Grade down gives the positions that would put the major cells in descending order. Reading those positions sorts the array, as [`>`](https://bpl.fast.ai//glyphs/greater-than.html) does directly.

```bpl
⍒33 11 44 66 22
```

```output
[3 2 0 4 1]ₓ
```

```bpl
a←33 11 44 66 22
```

```bpl
a.[⍒a]
```

```output
66 44 33 22 11
```

```bpl
[⍒a]⌷a
```

```output
66 44 33 22 11
```

```bpl
>a
```

```output
66 44 33 22 11
```

Grade is useful when we sort one array by another. Here the names go in order of decreasing score:

```bpl
names←"Sue" "Ali" "Bo"
scores←75 92 83
[⍒scores]⌷names
```

```output
"Ali" "Bo" "Sue"
```

The inverse, `⍒⁻`, takes a permutation: the positions from 0 to one less than the length, each used once. It returns one vector whose Grade down is that permutation, rather than recovering the original values:

```bpl
ranks←⍒⁻⍒a
ranks
```

```output
[2 0 3 4 1]ₓ
```

```bpl
⍒ranks
```

```output
[3 2 0 4 1]ₓ
```

#### dyadic [`⍒`](https://bpl.fast.ai//glyphs/del-stile.html) (Dyadic Grade Down)

A character left argument gives a custom alphabet. Here `c`, then `g`, then `f` have increasing positions in that alphabet. Unlisted characters share a position after the listed ones. Grade down puts these positions in reverse order:

```bpl
a←"abcdefgabcdefg"
```

```bpl
b←"cgf"⍒a
```

```bpl
[b]⌷a
```

```output
"abdeabdeffggcc"
```

### [`⍋`](https://bpl.fast.ai//glyphs/delta-stile.html) (Grade Up)

#### monadic [`⍋`](https://bpl.fast.ai//glyphs/delta-stile.html) (Grade Up)

Grade up gives the positions that would put the major cells in ascending order. Ties keep their original order.

```bpl
⍋33 11 44 66 22
```

```output
[1 4 0 2 3]ₓ
```

```bpl
a←33 11 44 66 22
```

```bpl
a.[⍋a]
```

```output
11 22 33 44 66
```

```bpl
[⍋a]⌷a
```

```output
11 22 33 44 66
```

Grade up's inverse likewise returns a vector whose Grade up is the given permutation:

```bpl
ranks←⍋⁻⍋a
ranks
```

```output
[2 0 3 4 1]ₓ
```

```bpl
⍋ranks
```

```output
[1 4 0 2 3]ₓ
```

#### dyadic [`⍋`](https://bpl.fast.ai//glyphs/delta-stile.html) (Dyadic Grade Up)

With a custom alphabet, Grade up puts `c`, `g`, then `f` first:

```bpl
a←"abcdefgabcdefg"
b←"cgf"⍋a
[b]⌷a
```

```output
"ccggffabdeabde"
```

#### Sorting each column

A grade along a named axis keeps the other axis names. Here `grades` holds row positions for each column. When `grades` is used as a selector, its `"row"` axis selects rows and its `"col"` axis pairs those positions with the matching columns:

```bpl
scores←["row":3 "col":4]ρ[3 1 4 1◇5 9 2 6◇5 3 5 8]
scores
```

```output
3 1 4 1
5 9 2 6
5 3 5 8
```

```bpl
grades←⍋⍠"row" scores
grades
```

```output
[0 0 1 0
 1 2 0 1
 2 1 2 2]ₓ
```

```bpl
scores.[grades]
```

```output
3 1 2 1
5 3 4 6
5 9 5 8
```

[Previous: Nested arrays and trains](nested-arrays.md) · [Study guide](../glyphs-study.md) · [Next: Rearranging and transforming arrays](rearranging-arrays.md)
