Assignment: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(added examples and explanation to index assignments)
 
m (fixed indentation)
Line 4: Line 4:
     mat←(1 2 3)(1 2 3)
     mat←(1 2 3)(1 2 3)
     mat
     mat
┌─────┬─────┐
┌─────┬─────┐
│1 2 3│1 2 3│
│1 2 3│1 2 3│
└─────┴─────┘
└─────┴─────┘
     mat[0]←1  ⍝ indexed assignment
     mat[0]←1  ⍝ indexed assignment
     mat
     mat
┌─┬─────┐
┌─┬─────┐
│1│1 2 3│
│1│1 2 3│
└─┴─────┘
└─┴─────┘
     mat←3 3⍴⍳9
     mat←3 3⍴⍳9
     mat
     mat
0 1 2
0 1 2
3 4 5
3 4 5
6 7 8
6 7 8
     mat[0 1;]  ⍝ 1 semicolon is necessary when dealing with 2D arrays, 2 semicolons for 3D arrays etc.
     mat[0 1;]  ⍝ 1 semicolon is necessary when dealing with 2D arrays, 2 semicolons for 3D arrays etc.
0 1 2
0 1 2
3 4 5
3 4 5
     mat[0 1;0 1]←0
     mat[0 1;0 1]←0
     mat
     mat
0 0 2
0 0 2
0 0 5
0 0 5
6 7 8
6 7 8
     ⍝ incrementing (or any dyadic function) parts of an array
     ⍝ incrementing (or any dyadic function) parts of an array
     mat←3 3⍴0
     mat←3 3⍴0
     mat
     mat
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
     mat[0 1;1]+←1
     mat[0 1;1]+←1
     mat
     mat
0 1 0
0 1 0
0 1 0
0 1 0
0 0 0
0 0 0
     mat[1;1],←'x'
     mat[1;1],←'x'
     mat
     mat
0 1 0
0 1 0
0 x 0
0 x 0
0 0 0
0 0 0

Revision as of 00:51, 13 February 2022

You can assign a value to a variable with the glyph: '←'.

Common examples (boxing on, and ⎕io is 0):

   mat←(1 2 3)(1 2 3)
   mat
┌─────┬─────┐
│1 2 3│1 2 3│
└─────┴─────┘
   mat[0]←1  ⍝ indexed assignment
   mat
┌─┬─────┐
│1│1 2 3│
└─┴─────┘
   mat←3 3⍴⍳9
   mat
0 1 2
3 4 5
6 7 8
   mat[0 1;]  ⍝ 1 semicolon is necessary when dealing with 2D arrays, 2 semicolons for 3D arrays etc.
0 1 2
3 4 5
   mat[0 1;0 1]←0
   mat
0 0 2
0 0 5
6 7 8
   ⍝ incrementing (or any dyadic function) parts of an array
   mat←3 3⍴0
   mat
0 0 0
0 0 0
0 0 0
   mat[0 1;1]+←1
   mat
0 1 0
0 1 0
0 0 0
   mat[1;1],←'x'
   mat
0 1 0
0 x 0
0 0 0