Search and Replace
6 posts
• Page 1 of 1
Search and Replace
I have a regex search and replace problem where I am searching with a single pattern, manipulating the results of each hit (but all at the same time) to produce a replacement for each hit, and then doing the replacement. I have the following solution:
In my use case, the replacement strings must be computed all together at one time, not serially inside the transformation function.
As you can see, I am using a transformation function, and stuffing in a counter to access the corresponding precomputed replacement item. This seems somewhat of a kludge, and I wonder am I missing a more direct solution. Also, this requires that the namespace provided to the transformation function is always the same instance.... which I'm not sure is a documented feature.
Is there a better solution for this?
- Code: Select all
⎕CR 'foo'
foo←{
p←'@.+?@' ⍝ Search pattern
x←(p ⎕S'&')⍵ ⍝ Search for expressions
0=≢x:⍵ ⍝ None
r←'Result'∘,¨⍕¨⍳≢x ⍝ Dummy result - values corresponding to x
f←r∘{ ⍝ Transformation function
⍵.i←{6::0 ⋄ 1+⍵.i}⍵
⍵.i⊃⍺
}
(p ⎕R f)⍵ ⍝ Do the replacement
}
foo 'This is A: @compute A@ ' 'This is B: @comp B@ and C: @comp C@'
This is A: Result0 This is B: Result1 and C: Result2
In my use case, the replacement strings must be computed all together at one time, not serially inside the transformation function.
As you can see, I am using a transformation function, and stuffing in a counter to access the corresponding precomputed replacement item. This seems somewhat of a kludge, and I wonder am I missing a more direct solution. Also, this requires that the namespace provided to the transformation function is always the same instance.... which I'm not sure is a documented feature.
Is there a better solution for this?
- paulmansour
- Posts: 420
- Joined: Fri Oct 03, 2008 4:14 pm
Re: Search and Replace
How about:
- Code: Select all
foo←{
p←'@.+?@' ⍝ Search pattern
x←(p ⎕S'&')⍵ ⍝ Search for expressions
0=≢x:⍵ ⍝ None
r←'Result'∘,¨⍕¨⍳≢x ⍝ Dummy result - values corresponding to x
f←{ ⍝ Transformation function
(r↓⍨←1)⊢⊃r ⍝ fetch first result and chop list
}
(p ⎕R f)⍵ ⍝ Do the replacement
}
-
Adam|Dyalog - Posts: 135
- Joined: Thu Jun 25, 2015 1:13 pm
Re: Search and Replace
Only a guess -
Paul probably supplied "r" as a composed argument because there was a time when a local referenced in an embedded dfn passed to a defined operator would not have been available. It was necessary to bundle everything required in the arguments.
Admittedly ⎕R and ⎕S are a sort of primitive where it was never a problem.
Even now, having used the technique on a number of occasions, I find it disquieting and hesitate to repeat it.
Paul probably supplied "r" as a composed argument because there was a time when a local referenced in an embedded dfn passed to a defined operator would not have been available. It was necessary to bundle everything required in the arguments.
Admittedly ⎕R and ⎕S are a sort of primitive where it was never a problem.
Even now, having used the technique on a number of occasions, I find it disquieting and hesitate to repeat it.
-
Phil Last - Posts: 628
- Joined: Thu Jun 18, 2009 6:29 pm
- Location: Wessex
Re: Search and Replace
Adam, thanks, that's very nice.
Phil, you give me too much credit, that's the only way I could figure out how to do it!
I do have an question for the dfn afficionados:
In Adam's solution, is the assignment destructive? In general is selective assignment of a variable in a dfn destructive or would memory issues ensue with a large r in the above example?
Phil, you give me too much credit, that's the only way I could figure out how to do it!
I do have an question for the dfn afficionados:
In Adam's solution, is the assignment destructive? In general is selective assignment of a variable in a dfn destructive or would memory issues ensue with a large r in the above example?
- paulmansour
- Posts: 420
- Joined: Fri Oct 03, 2008 4:14 pm
Re: Search and Replace
In dfns, selective, modified, and/or indexed assignment do not introduce a new frame. Only normal assignments do. So, yes, they are "destructive".
-
Adam|Dyalog - Posts: 135
- Joined: Thu Jun 25, 2015 1:13 pm
Re: Search and Replace
Good to know. Thanks Adam.
- paulmansour
- Posts: 420
- Joined: Fri Oct 03, 2008 4:14 pm
6 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group