[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-apl] West way to remove substrings?
From: |
enztec |
Subject: |
Re: [Bug-apl] West way to remove substrings? |
Date: |
Wed, 15 Mar 2017 17:44:36 -0600 |
something i had from a while back
#! rmsubstr
∇rs←substr rmsubstr string ⍝ " " quotes required for all strings and substrings
⍝ test variables - names for ease in testing
s1←string1←"abcdefghijkcdecef"
s2←string2←"abcdefghijkcdecefa"
s3←string3←"a" ⍝ single element string
ss1←substr1←"cde" ⍝ present in string 2x returns string with substring removed
ss2←substr2←"cee" ⍝ substring not there returns full string
ss3←substr3←"efa" ⍝ substring there in s2/string2 but not s1/string1
ss4←substr4←"a" ⍝ single element substr
ss4←substr5←"" ⍝ substring not there returns full string (special test for
0=⍴,substring)
r←(Rs←⍴string)-¯1+Rss←⍴substr
⍎((0=Rs)∨(0=Rss))/'''either string and/or substring are not given'' ◊
rs←string ◊ →0'
⍎(Rss>Rs)/'''substring is bigger then string'' ◊ rs←string ◊ →0'
⍝ 'r : ', r, ' Rs : ', Rs, ' Rss : ', Rss
ssa←((r,Rss)⍴substr)
⍝ 'ssa : '
⍝ ssa
sa←(string[((r,Rss)⍴¯1+⍳Rss)+⍉(Rss,r)⍴⍳r])
⍝ 'sa : '
⍝ sa
'string : ', string ⍝ comment for use as working function
'substring to remove from string : ', substr ⍝ comment for use as working
function
di←(Rss=+/ssa=sa)/⍳r
⍝ di←(Rss=ssa+.=sa)/⍳r ⍝ inner product fails here but should work ?
⍎(0=⍴di)/'rs←string ◊ →0' ⍝ substring not found - intact string returned
o←Rs⍴1
o[,(((⍴di), Rss)⍴¯1+⍳Rss)+⍉(Rss,⍴di)⍴di]←0
rs←o/string
∇
On Tue, 14 Mar 2017 22:20:42 +0800
Elias Mårtenson <address@hidden> wrote:
> In a discussion on the #lisp IRC channel recently, someone wanted to remove
> a certain substring from a larger string. In Lisp, the solution is to
> simply use cl-ppcre and remove by regex.
>
> But, I started thinking about how to do this in APL, and this is what I
> came up with:
>
> RemoveSubstrings ← {,/ ⍵ ⊂⍨ ↑~∨/ (-⍳⍴⍺) °.⌽ ⊂⍺⍷⍵}
>
> Is this a good way of solving the problem? I realise that I have a bug when
> the resulting string is empty:
>
> * "foo" RemoveSubstrings "foo"*
> DOMAIN ERROR
> λ1[1] λ←,/⍵⊂⍨↑∼∨/(-⍳⍴⍺)°.⌽⊂⍺⋸⍵
> ^ ^
>
> Does anyone have a better solution?
>
> Regards,
> Elias