Skip to content

Assigning to arrays

Indexed Assignment

Assign values at specified indices.

      t←4 4⍴'some sample text'
      t[⍸t∊'aeiou']←'!'

Selective Assignment

Define n←5 5⍴⍳25 in your workspace.

  1. Using selections, find at least four different ways to set the bottom-right 3 by 3 submatrix in n to 0. For example, (2 2↓n)←0.

    Hint

    See which primitives may be used in a selective assignment

Modified Assignment

Experiment with the following expressions, paying particular attention to the name f← array construct.

      salaries←18250 42500 56000 57250 48640
      codes←'ACDDC'
      salaries×←1.1
      salaries[⍸codes='C']×←1.05

      a←⎕A
      (3↑a),←'abcd'

At

Monadic functions take a single right argument array as input. Dyadic functions take two argument arrays.

Monadic operators take a single left operand which can be a function or an array (as in +/ where plus + is the function operand and reduce / is the operator).

Dyadic operators take two operands which could be functions or arrays depending on the operator's definition. For example, the rank operator F⍤k takes a function left operand F and array right operand k of up to 3 elements.

Selective and indexed assignment methods will change the values of variables. The "at" operator @ merges two arrays at specified indices and returns a new array.

If a function right operand returns a boolean array when applied to (e.g. 3=1 3 5) then ones 1 in the boolean array determine where scalars of are inserted.

      ('∆⍥'@{⍵∊'AEIOU'})2 3⍴'DYALOG'
      (' '@2 3 4)'DYALOG'
      (' '@(1 2)(1 3)(2 1))2 3⍴'DYALOG'
  1. The following expression contains an error:
    ('∆⍥'@1)2 3⍴'DYALOG'
    Change the parenthesised function containing @ in two ways so that it gives the following results:
    1. ∆∆∆
      LOG

    2. ∆∆∆
      ⍥⍥⍥

Generally, the left operand to @ is a function applied to scalars in which are specified by a right operand that is either an array of scalar (simple or enclosed vector) indices or a boolean array returned by a right operand function. An array left operand is shorthand for a constant function that returns the array.

      {1↓(1∘(819⌶)@{¯1⌽' '=⍵})' ',⍵}'my excellent heading'

Strand Assignment

Distributed assignment or strand assignment allows multiple names to be defined using a single assignment arrow .

      (max min avg)←{(⌈⌿⍵)(⌊⌿⍵)((+⌿÷≢)⍵)}3 1 4 1 5

Note

Strand assignment does not require names to be parenthesised, but we strongly recommend it for clarity.

We can assign items in nest to the three variables s←'A' v←1 2 3 and m←3 3⍴⍳9 using a single assignment arrow.

      nest←('A'(1 2 3))(3 3⍴⍳9)
      ((s v) m)←nest

Warning

You might have some issues when using inline, modified or strand assignment in dfns. This is by design, but can be a source of confusion.

      {a←3 ⋄ f←+ ⋄ a f←3 ⋄ a}⍬
3
      a←3 ⋄ f←+ ⋄ a f←3 ⋄ a
6

You can get around these problems by writing ∘⊢ (or in 12.1: ∘{⍵} ) to the immediate right of any function involved:

      {a←3 ⋄ f←+ ⋄ a f∘{⍵}←3 ⋄ a}⍬
6