array ← (from repl) ##.subs array ⍝ Vector substitution.
NB: this function is largely superseded by system operator ⎕R.
The left argument is a 2-item vector of 'find' and 'replace' vectors or scalars.
Occurrences of [from] along the last axis of the right argument are replaced
with [repl]. When [from] and [repl] are of differing lengths, shorter vectors
along the last axis are padded on the right with prototypical items.
Subs is an alternative to →ss← and →ssmat← with the following advantages:
- The right argument may be of any rank.
- It is a slightly quicker in many cases.
- Having the substitution pair on the left allows:
- Composition of a substition pair to form a new monadic function.
- Multiple substitutions using the primitive reduce operator (/).
- [from] and [repl] may be nested arrays.
Note that regular expressions are not used; there are no special characters, so
'*', '[]', etc. are taken literally. Notice also that in the case of overlapp-
ing strings, both strings are identified and both are replaced.
Examples:
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
See also: ss ssmat select
Back to: contents
Back to: Workspaces