## Selective specification

A number of APL functions can be used to select elements or portions of an array. These selection operations can also be used as specifications when enclosed in parentheses and used as the left argument to the assignment symbol. The array being selected must appear as the rightmost name within the parentheses. The following functions can be used to make the selection, either singly or in combinations.

Monadic ¹ † , ² ³ ´
Dyadic † ‡ œ ½ ² ³ ´ Þ

and the functions \ ('expand') and / ('compress', 'replicate').

Bracket indexing can also be used as the left argument to the assignment arrow although in this case it is not necessary to enclose the indexing expression in parentheses.

Some examples will illustrate.

First, bracket indexing:

                 TAB„2 3½¼6              (Simple matrix)
                 TAB[2;1]„8              (Row 2 column 1 assigned the value 8)

Nearly all the selection functions listed above operate on the outermost structure of a nested array. The shape of the right argument to the assignment arrow must either match that of the selected elements or be a scalar in which case scalar extension applies.

                 VEC„¼5
                 (3†VEC)„'ABC'           (First three elements become 'ABC')
                 VEC
           ABC 4 5
                 (3†VEC)„'A'             (A scalar right argument is extended to
                 VEC                      all items specified)
           AAA 4 5
                 MAT„3 4½'ABCDEFGHIJKL'   (Simple character matrix)
                 (,MAT)„'NEW DATAHERE'   (Ravel used for selection, so vector
                 MAT                      used as right argument to „)
           NEW
           DATA
           HERE
                 (('A'=,MAT)/,MAT)„'*'   (Combination of compression and ravel
                 MAT                      used for selection)
           NEW
           D*T*
           HERE
                 (,2 2†MAT)„'ŒŒŒŒ'       (Combination of † and , used for
                 MAT                      selection)
           ŒŒW
           ŒŒT*
           HERE
                 TABLE„3 4½¼12
                 TABLE
           1  2  3  4
           5  6  7  8
           9 10 11 12
                 (1 0 1 0/TABLE)„3 2½100 (Compression used for selection)
                 TABLE
           100  2 100  4
           100  6 100  8
           100 10 100 12
                 DATA„¼13
                 X„10 20 30              (Other APL functions may be used within
                 ((½X)†DATA)„X            the parentheses - here ½ is used to supply
                 DATA                     the left argument to †)
           10 20 30 4 5 6 7 8 9 10 11 12 13
                 Y„¼10
                 X„3
                 ((2+X)†Y)„²¼X+2         (Left argument to † includes the + function)
                 Y
           5 4 3 2 1 6 7 8 9 10

The function enlist (¹) removes all nesting from an array. When used with selective specification, it can be used to replace elements at the deepest level of nesting, whilst retaining the structure of the array.

                 NEST„(2 2½¼4) 'TEXT' (3 1½¼3)
                 NEST
            1 2   TEXT   1
            3 4          2
                         3
                 (¹NEST)„0               (Entire array set to 0, but original
                 NEST                     structure retained)
            0 0   0 0 0 0   0
            0 0             0
                            0
          (6Þ¹NEST)„999                  (Single element at bottom of nested array
          NEST                            array altered)
     0 0   0 999 0 0   0
     0 0               0
                       0
          (7Þ¹NEST)„›'TEXT'              (Further nesting introduced)
          NEST
     0 0    0 999 TEXT 0    0
     0 0                    0
                            0

The function first (†), selects the whole array which is the first element in an array. If first is used purely to select the first array within a nested array, then the array which is the right argument to the assignment arrow will replace the selected array.

                 (†NEST)„'ABC'           (First element of NEST is a matrix of
                 NEST                     shape 2 by 2. This is replaced by
            ABC  0 999 TEXT 0    0        a length 3 vector)
                                 0
                                 0
                 (2Þ†NEST)„'Œ'           (One element within the first element of
                 NEST                     NEST is replaced)
            AŒC  0 999 TEXT 0    0
                                 0
                                 0

Pick (œ) will select an entire array at an arbitrary depth in a nested array, and will also replace that entire array by the right argument to the specification symbol.

                 2 2œNEST
           999
                 (2 2œNEST)„¼10          (New array placed in 2 2œ of NEST)
                 NEST
            AŒC  0  1 2 3 4 5 6 7 8 9 10  TEXT 0    0
                                                    0
                                                    0
                 (2œNEST)„'DATA'         (Nested vector at element 2 replaced by
                 NEST                     length 4 vector)
            AŒC DATA   0
                       0
                       0
                 (3 (2 1)œNEST)„1000     (Row 2 column 1 of element 3
                 NEST                     specified)
            AŒC DATA      0
                       1000
                          0

There are some exceptions and restrictions to the rules for selective specification:

- User-defined functions and operators cannot be used within selective specification

- Execute (–) is not allowed within selective specification

- System functions are allowed within selective specification with the exception of those which use execute (ŒEA and ŒEC)

- The selection expression must select elements from the variable and not insert fill items (as, for example, can be done by expand and replicate).

- No arithmetic operations can be carried out on the array being specified or on the elements selected

- Assignments are not allowed within the parentheses used for selective specification.

- Selective and multiple specification operations cannot be mixed.

Thus, if

                 X„3 4 5

then the following expressions are not allowed:

                 (AVERAGE X)„6           (Use of user-defined function)

                 ((–'1+2')†X)„'ABC'      (Use of execute)

                 ((Y„2)‡X)„'A'           (Assignment within the selection expression)

                 ((2‡X) Y Z)„'ABC'       (Mixture of multiple and selective
                                          specifications)
                 (10†X)„¼10              (Fill items inserted by the selection
                                          expression)

As stated above, arithmetic may not be carried out on the elements of an array that are selected:

                 X„¼5
                 (2+1†X)„5
           DOMAIN ERROR
           (2+1†X)„5
                ^

but other expressions within the specification parentheses may use arithmetic operations, even on another instance of the name being specified:

                 ((2+1†X)†X)„100
                 X
           100 100 100 4 5

Topic: APLX Help : Help on APL language : APL Fundamentals : Selective specification

[ Previous | Next | Contents | Index ]