# Variables and Indexing (4 of 9)

Indexing selects an element from data by reference to its position in the data. One number is sufficient to identify an element in a vector. Two numbers - the row number and the column number - select an element in a matrix. Further numbers are necessary for data of more dimensions.There are two alternative methods to index data - using the `[]` brackets or the `⌷` **index** function.

## Bracket Indexing

Bracket indexing can be used to select elements from an array, for example one or more elements from a vector, or individual rows or columns of a matrix.

The index or indices are enclosed in square brackets, each dimension being separated by a semicolon. If no number is used for a particular dimension, then all the elements in that dimension are selected. APL allows index references to start either at 0 or 1. The index origin (which is controlled by `⎕IO` or `)ORIGIN`) determines whether index positions start from 1 or 0. In the examples below the convention of index origin 1 is used.

LIST←12 24 36 48 LIST[2] (Selects the second item in LIST) 24 LIST[1]+LIST[4] (Adds the first and fourth items in LIST) 60 ALF←'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ALF[26 1 13 2 9 1] (Selects the letters in ZAMBIA ZAMBIA from the contents of ALF) TABLE (TABLE consists of 2 rows and 4 columns) 10 20 30 40 50 60 70 80 TABLE[1;4] (Selects the item in row 1, column 4) 40 TABLE[1;1 2 3 4]+TABLE[2;1 2 3 4] 60 80 100 120 (Adds the 4 columns in row 1 to the 4 columns in row 2) TABLE[1;]+TABLE[2;] (Shorthand way of doing the same 60 80 100 120 operation as in the last example)

In general, the indices may be of any shape or rank, so long as each of their elements correspond to valid elements within the array being indexed. The shape of the result of an indexing operation is generated by the shape of the index arrays. Thus

⍴TABLE[A;B] (Where A and B are arrays)

is identical to

(⍴A),⍴B

This has the important consequence, that if all the indexing arrays are in fact scalars, the result is also a scalar. Similarly, any axis of an array indexed by a scalar generates a result in which that axis does not exist.

ALF[2 2⍴⍳4] (ALF indexed by a matrix) AB (Result is a matrix) CD ⍴TABLE[1;1 2 3 4] (Rows indexed by a scalar, result is 4 a vector) ⍴TABLE[,1;1 2 3 4] (Rows indexed by a vector, result is 1 4 a matrix) ⍴TABLE[1 1⍴1;1 2 3 4] (Rows indexed by a matrix, 1 1 4 result is a three dimensional array

## The ⌷ (index) function

An alternative to bracket indexing is the `⌷` (**index**) function. The index specification is given as the left argument to the `⌷` function and is equivalent to bracket indexing in that

ROW COL ⌷ MATRIX

and

MATRIX[ROW;COL]

are equivalent. Although arguably less readable than bracket indexing, the index function has the advantage that it is syntactically consistent with other APL primitive functions, and can thus be used with operators such as **Each** (¨).

Each element in the left argument addresses successive dimensions of the right argument and multiple index selections may be formed by creating a suitably nested vector. The dimensions specified in the left argument are used in the same order as with the `⍴` function, that is columns last, preceded by rows and so on. Index is affected by the Index Origin (`⎕IO`).

2 ⌷ 1 2 3 4 5 (Scalar for vector indexing - only one 2 dimension) (⊂3 4)⌷ 1 2 3 4 5 (Nested scalar for multiple index) 3 4 TAB←2 5⍴⍳10 TAB 1 2 3 4 5 6 7 8 9 10 2 3 ⌷ TAB 8 2 (2 3)⌷ TAB (Second element of the indexing vector 7 8 is the enclosed vector 2 3) (1 2) (2 3)⌷TAB (Nested 2 element vector for multiple 2 3 index. Result is rows 1 2 and columns 7 8 2 3)

If the index function is given an empty left argument, and a scalar right argument, it will return the scalar as the result.

(⍳0)⌷37 37