# 3A. Indexing

encompasses everything performed by bracket indexing in other languages. The ranks are 0 _  , that is, x{y can be considered individually for each scalar in x and for y in toto, with the overall result constructed from the individual results in the same way as for all other functions. For scalar x then:

>x is a scalar or a list, and r=:>j{,>x  are the indices for axis j . r may be integers in the range i.&.(+&n)n=:j{\$y ,  which selects cells n|r , or r  may be boxed integers in that range, in which case the selected cells are all except those in n|r . Thus:

```   z=: 0{y=: 3 3 3\$'ABCDEFGHIJKLMNOPQRSTUVWXYZ]'
ibbb=: <ibb=: <ib=: <i=: 1 _1
jbbb=: <jbb=: <jb=: <j=: 2 1
ijbbb=: <ijbb=: <ijb=: <ij=: 2 2\$i,j
(] ; i&{ ; ib&{ ; ibb&{ ; ibbb&{) z
+---+---+-+---+---+
|ABC|DEF|F|DEF|ABC|
|DEF|GHI| |GHI|   |
|GHI|   | |   |   |
+---+---+-+---+---+

ijb{y
|rank error
|   ijb    {y

(] ; i&{ ; ijbb&{ ; ijbbb&{) z
+---+---+---+---+
|ABC|DEF|DEF|ABC|
|DEF|GHI|GHI|   |
|GHI|   |   |   |
|   |   |GHI|   |
|   |   |DEF|   |
+---+---+---+---+```

The amend adverb } applied to an index produces a function that replaces the selected part of the right argument by the left argument. For example:

```   '*' ib} z
ABC
DE*
GHI

('def',:'ghi') i} z
ABC
def
ghi

(] ; i&{ ; ib&{ ; ibb&{ ; ibbb&{)"2 y
+---+---+-+---+---+
|ABC|DEF|F|DEF|ABC|
|DEF|GHI| |GHI|   |
|GHI|   | |   |   |
+---+---+-+---+---+
|JKL|MNO|O|MNO|JKL|
|MNO|PQR| |PQR|   |
|PQR|   | |   |   |
+---+---+-+---+---+
|STU|VWX|X|VWX|STU|
|VWX|YZ]| |YZ]|   |
|YZ]|   | |   |   |
+---+---+-+---+---+

\$(<<'') { z
0 3

\$(<a:) { z
0 3```

Indexing on higher-rank arrays may be illustrated by the argument y :

```   ]k=: <1 2;a:;0 2
+------------+
|+---+--+---+|
||1 2|++|0 2||
||   ||||   ||
||   |++|   ||
|+---+--+---+|
+------------+

y ; k{y
+---+--+
|ABC|JL|
|DEF|MO|
|GHI|PR|
|   |  |
|JKL|SU|
|MNO|VX|
|PQR|Y]|
|   |  |
|STU|  |
|VWX|  |
|YZ]|  |
+---+--+```

The following examples further illustrate the use of the indexing function. For each example, it may be instructive to plug the values into the expression r=:>j{,>x and work out the result.

 n0=: y=: i.4 5 6 7 Array used in examples n1=: (<,<3){y Item 3 of y n2=: (<,3){y Item 3 of y n3=: (<3){y Item 3 of y n4=: 3{y Item 3 of y n5=: (<,<_1){y The last item of y (item _1 of y) n6=: (<,_1){y The last item of y (shape 5 6 7) n7=: (<_1){y The last item of y n8=: _1{y The last item of y n9=: (_1+#y){y The last item of y n10=: 0{y The first item of y n11=: (-#y){y The first item of y n12=: 3 0 _2 0{y Items 3 0 _2 0 of y n13=: i=: ?2 3\$0{\$y Indices used in examples n14=: j=: ?    1{\$y Indices used in examples n15=: k=: ?7  \$2{\$y Indices used in examples n16=: (

JPhrases/Indexing (last edited 2009-09-02 00:05:33 by RicSherlock)