Search and Replace

General APL language issues

Search and Replace

Postby paulmansour on Fri Jun 01, 2018 9:22 pm

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:


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

Postby Adam|Dyalog on Sun Jun 03, 2018 4:52 am

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
 }
User avatar
Adam|Dyalog
 
Posts: 135
Joined: Thu Jun 25, 2015 1:13 pm

Re: Search and Replace

Postby Phil Last on Mon Jun 04, 2018 11:48 am

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.
User avatar
Phil Last
 
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Search and Replace

Postby paulmansour on Mon Jun 04, 2018 12:03 pm

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?
paulmansour
 
Posts: 420
Joined: Fri Oct 03, 2008 4:14 pm

Re: Search and Replace

Postby Adam|Dyalog on Mon Jun 04, 2018 12:28 pm

In dfns, selective, modified, and/or indexed assignment do not introduce a new frame. Only normal assignments do. So, yes, they are "destructive".
User avatar
Adam|Dyalog
 
Posts: 135
Joined: Thu Jun 25, 2015 1:13 pm

Re: Search and Replace

Postby paulmansour on Mon Jun 04, 2018 12:48 pm

Good to know. Thanks Adam.
paulmansour
 
Posts: 420
Joined: Fri Oct 03, 2008 4:14 pm


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest