[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-apl] Not a bug, need help coding search&replace on a vector
From: |
Xiao-Yong Jin |
Subject: |
Re: [Bug-apl] Not a bug, need help coding search&replace on a vector |
Date: |
Tue, 21 Jun 2016 22:30:38 -0500 |
Here is the dyalog ⎕R (replace), you can find ⎕S too. It’s a nice interface,
but not easy to remember everything.
http://help.dyalog.com/14.1/Content/Language/System%20Functions/r.htm
> On Jun 21, 2016, at 10:10 PM, Christian Robert <address@hidden> wrote:
>
> Oh yes, would be nice. (pcre would be as nice as regex, may be more)
>
> make it a ⎕regex or ⎕pcre so, no need to load a specific .so file or )lib to
> use it ;-)
>
> Ideal would be to be able to not only scan but be able to compile a pcre,
> find patterns, search & replace ...
>
> I am dreaming, but still awake ...
>
> Xtian.
>
>
> On 2016-06-21 22:39, Elias Mårtenson wrote:
>> Would anyone be interested if I actually implemented support regex by
>> calling into the C library?
>>
>> I'm envisioning something like this:
>>
>> * "^([a-z]+):([0-9]+)$" regex∆scan "foo:123"*
>> ┏→━━━━━━━━━━┓
>> ┃"foo" "123"┃
>> ┗∊━━━━━━━━━━┛
>>
>> Regards,
>> Elias
>>
>> On 22 June 2016 at 10:35, Xiao-Yong Jin <address@hidden
>> <mailto:address@hidden>> wrote:
>>
>> At some point I really wanted to implement the simplest editor, namely ed
>> (it would be much better than any APL system has offered), in APL, but I
>> never finished the regex part. Perhaps it would be a nice exercise for
>> someone who’s interested in learning APL to actually implement some of the
>> most used Unix command line utilities.
>>
>> > On Jun 21, 2016, at 8:53 PM, Christian Robert
>> <address@hidden<mailto:address@hidden>> wrote:
>> >
>> > Hi,
>> >
>> > A more general purpose search&replace (some bug fixed), take (2), was
>> "snr".
>> >
>> >
>> >
>> > )sic
>> > )erase replace
>> > ∇z←s replace p;fr;to;P;i;⎕io
>> > ⍝ -------------
>> > →(2≤≡s)/ForEach
>> > →(2≤⍴⍴s)/Matrix
>> > →Vector
>> > ⍝ -------------
>> > ForEach: z←{⍵ replace p}¨s ◊ →0
>> > Matrix: z←⊃ {⍵ replace p}¨ ⊂[2]s ◊ →0
>> > ⍝ -------------
>> > Vector: p←,¨p ◊ z←s←,s ⋄ ⎕io←1
>> > Loop:→((⍴p)<2)/0
>> > (fr to)←2↑p ◊ p←2↓p
>> > P←¯1↓∧/[1](¯1+⍳⍴fr)⌽fr∘.=z,⎕ucs 1
>> > i←0
>> > Sanitize: i←i+1
>> > →(i>(⍴P)-⍴fr)/Substitute
>> > →(0=P[i])/Sanitize
>> > ((⊂ i+¯1+⍳⍴fr)⌷P)←(⍴fr)↑1
>> > →Sanitize
>> > Substitute: P←⌽(P/⍳⍴P)
>> > ⊣ {z←((¯1+⍵)↑z),to,(¯1+⍵+⍴fr)↓z}¨P
>> > →Loop
>> > ∇
>> >
>> > It should be vector, matrix and embedded objects "aware" (as long as it
>> is all text)
>> >
>> > Syntax:
>> >
>> > source replace "this" "by_that" "this" "by_that" [...] (eg: pairs of
>> "from" and "to")
>> >
>> > Examples:
>> >
>> > (⎕cr 'replace') replace 'replace' 'snr' 'ForEach' "Each"
>> "Substitute" "Subst" "Sanitize" "Sane"
>> > z←s snr p;fr;to;P;i;⎕io
>> > ⍝ -------------
>> > →(2≤≡s)/Each
>> > →(2≤⍴⍴s)/Matrix
>> > →Vector
>> > ⍝ -------------
>> > Each: z←{⍵ snr p}¨s ◊ →0
>> > Matrix: z←⊃ {⍵ snr p}¨ ⊂[2]s ◊ →0
>> > ⍝ -------------
>> > Vector: p←,¨p ◊ z←s←,s ⋄ ⎕io←1
>> > Loop:→((⍴p)<2)/0
>> > (fr to)←2↑p ◊ p←2↓p
>> > P←¯1↓∧/[1](¯1+⍳⍴fr)⌽fr∘.=z,⎕ucs 1
>> > i←0
>> > Sane: i←i+1
>> > →(i>(⍴P)-⍴fr)/Subst
>> > →(0=P[i])/Sane
>> > ((⊂ i+¯1+⍳⍴fr)⌷P)←(⍴fr)↑1
>> > →Sane
>> > Subst: P←⌽(P/⍳⍴P)
>> > ⊣ {z←((¯1+⍵)↑z),to,(¯1+⍵+⍴fr)↓z}¨P
>> > →Loop
>> >
>> >
>> > You can even try "⎕fx" the result if it looks like Ok. (ps: always
>> check the result before committing)
>> >
>> > Substitutions are done in pair sequence for each
>> /vector/matrix/embedded obj.
>> >
>> > So replace order is *very* important.
>> >
>> > It must still have some bugs, report them to me, with examples if so.
>> Will do my best but no promises.
>> >
>> > Purpose: well, not sure, it was an exercise. I used it on itself to
>> finally rename the function and change some variables/labels names.
>> >
>> >
>> > Features: would be fun to pass parameters to each of the /from/to/ like
>> maximum number of substitutions to do per line ...
>> > whole words only (taking apl characters as blanks) ... so
>> variable "s" won't match last character of variable "pairs"
>> > ... no limits
>> >
>> >
>> > Xtian.
>> >
>> >
>> > On 2016-06-21 12:27, Juergen Sauermann wrote:
>> >> Hi Xtian,
>> >>
>> >> how about this:
>> >>
>> >> *
>> **s←"444This4is4a4444test44with44444lot4of4blanks4444at4beginning4and444end444444"**
>> >> from←"444"**
>> >> ** to←"5"**
>> >> **
>> >> ** ↑⍎')HOST echo "',s,'" | sed s/',from,'/',to,'/g'**
>> >> **5This4is4a54test44with544lot4of4blanks54at4beginning4and5end55**
>> >> ** **
>> >> *I have replaced the *^* in your example because it (and some other
>> characters) have a special meaning in
>> >> regular expressions. But the special characters can be easily quoted
>> in APL if needed, for example:
>> >>
>> >> *
>> s←"^^^This^is^a^^^^test^^with^^^^^lot^of^blanks^^^^at^beginning^and^^^end^^^^^^"
>> **
>> >> ** ((s='^')/s)←⊂'\^'**
>> >> ** ⊃,/s**
>> >>
>> **\^\^\^This\^is\^a\^\^\^\^test\^\^with\^\^\^\^\^lot\^of\^blanks\^\^\^\^at\^beginning\^and\^\^\^end\^\^\^\^\^\^**
>> >> *
>> >> /// Jürgen
>> >>
>> >>
>> >> On 06/21/2016 05:12 AM, Christian Robert wrote:
>> >>> Hi, it's not a bug but a request for help,
>> >>>
>> >>>
>> >>> suppose
>> s="^^^This^is^a^^^^test^^with^^^^^lot^of^blanks^^^^at^beginning^and^^^end^^^^^^"
>> >>> suppose from="^^^"
>> >>> suppose to="^"
>> >>>
>> >>> I need a function who can replace ("^^^" to "^"), or ("^" to
>> "^^^^^^^^") or ("" to "blabla" with limits of source size) ie: without
>> being lost in infinite loop.
>> >>>
>> >>>
>> >>>
>> >>> "s" is a character vector, "from" is a character vector, and "to" is
>> a character vector,
>> >>> both "s", "from" and "to" can be "".
>> >>>
>> >>>
>> >>>
>> >>> I'm pretty sure this idiom already exist.
>> >>>
>> >>> if you can help me, please do.
>> >>>
>> >>> Xtian.
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>>
>> >>
- [Bug-apl] Not a bug, need help coding search&replace on a vector, Christian Robert, 2016/06/20
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Louis Chretien, 2016/06/21
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Juergen Sauermann, 2016/06/21
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Christian Robert, 2016/06/21
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Christian Robert, 2016/06/21
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Xiao-Yong Jin, 2016/06/21
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Elias Mårtenson, 2016/06/21
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Christian Robert, 2016/06/21
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector,
Xiao-Yong Jin <=
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Christian Robert, 2016/06/21
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Elias Mårtenson, 2016/06/21
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Juergen Sauermann, 2016/06/22
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Louis Chretien, 2016/06/23
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Elias Mårtenson, 2016/06/23
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Xiao-Yong Jin, 2016/06/23
- Re: [Bug-apl] Not a bug, need help coding search&replace on a vector, Jay Foad, 2016/06/28
- [Bug-apl] Improvements on ⎕FIO, Christian Robert, 2016/06/21
- Re: [Bug-apl] Improvements on ⎕FIO, Elias Mårtenson, 2016/06/21
- Re: [Bug-apl] Improvements on ⎕FIO, Christian Robert, 2016/06/21