Topic: APLX Help : Help on APL language : APL Primitives : ⊃ Pick
[ Previous | Next | Contents | Index | APL Home ]

www.microapl.co.uk

Pick


Two-argument form  See also one-argument form Disclose

Pick is used to select an item from its right argument according to the specification contained in its left argument. Each element in the left argument is used to specify successively deeper selections in the right argument. At each level of specification the element in the left argument being used must be of the appropriate shape - a single number for a vector, a two element vector for a matrix and so on.

             A←'FIRST' 'SECOND' 'THIRD'
             ⍴A                      (Three element vector)
       3
             2⊃A                     (Pick the second element)
       SECOND
             2 3⊃A                   (Pick the third element of the second
       C                              element)
             A←(1 'FIRST') (2 'SECOND') (3 'THIRD')
             ⍴A                      (Three element vector, with each element
       3                              a two element vector)
             3⊃A
       3 THIRD                       (Third element selected)
             3 2⊃A
       THIRD                         (Second element of third element selected)
             3 2 1⊃A
       T                             (First element of second element of third
                                      element)

When operating on arrays with two or more dimensions, care must be taken to ensure that the left argument to is correctly formed.

             TABLE←2 2⍴(⍳3) 'NAMES' (2 2⍴4 5 6 7) (3 3⍴'ABCDEFGHI')
             TABLE
       1 2 3  NAMES
         4 5   ABC
         6 7   DEF
               GHI

Selection of one of the outermost items from TABLE must be by means of a two element vector (given the shape of TABLE is 2 2), but this selection item must be formed as a scalar to indicate that it refers to the outmost layer.

             1 2⊃TABLE
       RANK ERROR
             1 2⊃TABLE
             ^

In the example above, the left argument to pick is interpreted as 'first element from outermost layer' then 'second element from next layer deep'. A correctly formed left argument is:

             (⊂1 2)⊃TABLE
       NAMES
             (1 2) 2⊃TABLE
       A                             (Select row 1 column 2, then element 2)
             (2 1) (2 2)⊃TABLE       (Select row 2 column 1, then row 2
       7                              column 2)

Pick may be used with selective specification, in which case the whole array picked will be replaced by the object being assigned.


Topic: APLX Help : Help on APL language : APL Primitives : ⊃ Pick
[ Previous | Next | Contents | Index | APL Home ]