⍝ Vector substitution:
⎕io←1
dots←' ·'∘subs ⍝ Composition
dots 2 12⍴'Many a time and oft. ' ⍝ Single char substitution
Many·a·time·
and·oft.····
(6 7)(666 777)subs 2 3 4⍴⍳12 ⍝ High rank argument
1 2 3 4
5 666 777 8
9 10 11 12
1 2 3 4
5 666 777 8
9 10 11 12
(7 8 9) 'repl' subs 3 5⍴⍳15 ⍝ Replace with longer string
1 2 3 4 5 0
6 r e p l 10
11 12 13 14 15 0
(7 8 9) 'r' subs 3 5⍴⍳15 ⍝ Replace with shorter string
1 2 3 4 5
6 r 10 0 0
11 12 13 14 15
⍝ Multiple substitutions:
↑subs/('Mr' 'Mrs')('his' 'her')'Mr Green and his daughter, Theresa.'
Mrs Green and her daughter, Theresa.
⍕(3,¨2 3 4)('peek' 'a' 'boo') subs ⍳5 5 ⍝ Nested substitution
1 1 1 2 1 3 1 4 1 5
2 1 2 2 2 3 2 4 2 5
3 1 peek a boo 3 5
4 1 4 2 4 3 4 4 4 5
5 1 5 2 5 3 5 4 5 5
(1 1)(2 3)subs 0 1 1 1 0 ⍝ Overlapping strings
0 2 3 2 3 0
⍝∇ subs
Back to: code
Back to: Workspaces