Amend } (modify selected)

From J Wiki
< Help(Redirected from Help/Primer/Amend curlyrt)
Jump to navigation Jump to search


<=   =>

} (amend) is an adverb whose result is a dyad that is used to modify an array. The left argument of amend is usually a noun. Let's look at an example:

   change_index_two =. 2}


The verb change_index_two is used dyadically. Its right argument is the original data and the left argument is a new value for index position 2.

   15 change_index_two 5 6 7 8
5 6 15 8
   30 change_index_two 23 18 17
23 18 30
   'b' change_index_two 'cat'
cab
   15 (2}) 5 6 7 8
5 6 15 8
   30 (2}) 23 18 17
23 18 30
   'b' 2} 'cat'
cab


This extends in ways that you might expect.

   23 (1 4}) 7 7 7 7 7
7 23 7 7 23
   23 24 (1 4}) 7 7 7 7 7
7 23 7 7 24
   'bet' 2 5 8} 'cattumbiz'
cabtuebit


In general an amend x s} y is defined as:

The result is formed by replacing by x those parts of y that are selected by s. The s argument to } is treated the same way as the left argument of the verb { .

Amend allows us to give selected parts of an array new values. The amend argument gives the indexing information about what data to modify. This selects the same elements to be modified as it would if used as the left argument to the dyad verb { .

If you understand { (from), then amend is quite simple.

   m =. i. 3 4
   1 { m
4 5 6 7
   23 23 23 23 (1}) m
 0  1  2  3
23 23 23 23
 8  9 10 11
   23 (1}) m
 0  1  2  3
23 23 23 23
 8  9 10 11


You can first use the selection information to see what data is to be modified.

   1 2 { m
4 5  6  7
8 9 10 11


The selected data is a subarray of shape 2 4. So you need a subarray of shape 2 4 to replace the selected data.

  (2 4 $ 23 23 23 23 24 24 24 24) (1 2}) m
 0  1  2  3
23 23 23 23
24 24 24 24


Suppose you want to modify the subarray that is selected as rows 1 and 2, and columns 0 and 3 with the value 12.

   12 ((<1 2;0 3)}) m
 0 1  2  3
12 5  6 12
12 9 10 12


Modify that subarray by replacing it with the array in the left argument.

   (2 2 $ 23 24 25 26) ((<1 2;0 3)}) m
 0 1  2  3
23 5  6 24
25 9 10 26


<=   =>

Primer Index               Hover to reveal titles   -   Click to access   -   Current page is highlighted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
45 46 47 48
50 51 52 53 54 55 56 57
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
97 98 99 100 101 102 103 104 105 106