Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  nodelist  faq  login

Real programmers don't comment their code. It was hard to write, it should be hard to understand.


programming / comp.lang.tcl / Re: missing feature for "regsub"

SubjectAuthor
* missing feature for "regsub"aotto1968
+- Re: missing feature for "regsub"Arjen Markus
`* Re: missing feature for "regsub"Schelte
 `* Re: missing feature for "regsub"Schelte
  `- Re: missing feature for "regsub"Harald Oehlmann

1
Subject: missing feature for "regsub"
From: aotto1968
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Sun, 1 May 2022 12:59 UTC
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: aotto1...@t-online.de (aotto1968)
Newsgroups: comp.lang.tcl
Subject: missing feature for "regsub"
Date: Sun, 1 May 2022 14:59:57 +0200
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <t4m08e$3j6$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 1 May 2022 12:59:58 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="19aad355ec6e4c17de98df5c7b16c157";
logging-data="3686"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18CooY+VUda3JMfg/GAnL4KplvNvD4Su28="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:TmxHXBQ6PB7ywd5+aEIIrTEbbm8=
Content-Language: en-US
View all headers

Hi,

I don't know if the TCL community is the right place to post a Regular-Expression (RG) enhancement, but let me try :-)

problem: howto put MULTIPLE "regsub" statements into ONE single
(probably more efficient) together.

lets start with "string map…"

 > string map { old1 new1 old2 new2 … } STRING

ONE command is doing MULTIPLE replacement.

I want to do the same with "regsub" BUT with a RG string, proposal:

regsub {…(old1|old2|…)…} STRING {\?1(new1|new2|…)} OUTVAR

"old1" will be replaced with "new1" … etc

example: >>>>>

string="MyName§E other-thing"

regsub {(\w+)§(E|S|U)} $string {\?2(enum|struct|union) \1} OUTVAR

OUTVAR="enum MyName other-thing"

<<<<<<<<<<<<<<

new is the "\?2(…)"

\2 would replace the match-string and "\?2(…)" will use the
index of the match-string and pig-up the idx-replacement from the "(…}"


mfg




Subject: Re: missing feature for "regsub"
From: Arjen Markus
Newsgroups: comp.lang.tcl
Date: Mon, 2 May 2022 10:06 UTC
References: 1
X-Received: by 2002:a0c:cc08:0:b0:45a:8f81:d8a8 with SMTP id r8-20020a0ccc08000000b0045a8f81d8a8mr1813672qvk.88.1651485970274;
Mon, 02 May 2022 03:06:10 -0700 (PDT)
X-Received: by 2002:a54:468f:0:b0:322:5226:598e with SMTP id
k15-20020a54468f000000b003225226598emr5013452oic.213.1651485970015; Mon, 02
May 2022 03:06:10 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Mon, 2 May 2022 03:06:09 -0700 (PDT)
In-Reply-To: <t4m08e$3j6$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=136.231.213.70; posting-account=A91wAAoAAADgBUxBX6QqsrSD26GLhVp8
NNTP-Posting-Host: 136.231.213.70
References: <t4m08e$3j6$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <aa3897ff-c9d2-4bd6-a3fe-2d90dbad2476n@googlegroups.com>
Subject: Re: missing feature for "regsub"
From: arjen.ma...@gmail.com (Arjen Markus)
Injection-Date: Mon, 02 May 2022 10:06:10 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 50
View all headers
On Sunday, May 1, 2022 at 3:00:02 PM UTC+2, aotto1 wrote:
Hi,

I don't know if the TCL community is the right place to post a
Regular-Expression (RG) enhancement, but let me try :-)

problem: howto put MULTIPLE "regsub" statements into ONE single
(probably more efficient) together.

lets start with "string map…"

string map { old1 new1 old2 new2 … } STRING

ONE command is doing MULTIPLE replacement.

I want to do the same with "regsub" BUT with a RG string, proposal:

regsub {…(old1|old2|…)…} STRING {\?1(new1|new2|…)} OUTVAR

"old1" will be replaced with "new1" … etc

example: >>>>>

string="MyName§E other-thing"

regsub {(\w+)§(E|S|U)} $string {\?2(enum|struct|union) \1} OUTVAR

OUTVAR="enum MyName other-thing"

<<<<<<<<<<<<<<

new is the "\?2(…)"

\2 would replace the match-string and "\?2(…)" will use the
index of the match-string and pig-up the idx-replacement from the "(…}"


mfg
A feature of [string map] is that the substitutions are defined in pairs, whereas your suggested syntax interferes with the alternatives already defined in regular expressions. I suspect there are more interferences lurking in there, besides the obvious one. Is the benefit over a sequence of regsubs really worth the trouble? I am not an expert wrt the machinery of regular expression matching, but I can imagine this would be quite complicated to getright, even at the definition level.

Regards,

Arjen


Subject: Re: missing feature for "regsub"
From: Schelte
Newsgroups: comp.lang.tcl
Organization: KPN B.V.
Date: Mon, 2 May 2022 12:04 UTC
References: 1
Date: Mon, 2 May 2022 14:04:01 +0200
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1
Subject: Re: missing feature for "regsub"
Content-Language: en-US
Newsgroups: comp.lang.tcl
References: <t4m08e$3j6$1@dont-email.me>
From: nos...@wanadoo.nl (Schelte)
In-Reply-To: <t4m08e$3j6$1@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Message-ID: <nnd$50cb211c$6ea16057@c1bbf144b10410cd>
Organization: KPN B.V.
Path: i2pn2.org!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr1.eu1.usenetexpress.com!newsfeed.xs4all.nl!newsfeed9.news.xs4all.nl!news-out.netnews.com!news.alt.net!fdc2.netnews.com!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!abe005.abavia.com!abp001.abavia.com!news.kpn.nl!not-for-mail
Lines: 17
Injection-Date: Mon, 02 May 2022 14:04:01 +0200
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 1385
View all headers
On 01/05/2022 14:59, aotto1968 wrote:
problem: howto put MULTIPLE "regsub" statements into ONE single
(probably more efficient) together.

In Tcl 8.7, the regsub command has a new -command option that may be of use (tip #463: https://core.tcl-lang.org/tips/doc/trunk/tip/463.md

At least it can handle your example with a single regsub:

regsub -command {(\w+)§[ESU]} $string [list apply [list {match prefix} {
     set ch [string index $match end]
     string cat [string map {E enum S struct U union} $ch] " " $prefix
}]] OUTVAR


Schelte.


Subject: Re: missing feature for "regsub"
From: Schelte
Newsgroups: comp.lang.tcl
Organization: KPN B.V.
Date: Mon, 2 May 2022 12:12 UTC
References: 1 2
Date: Mon, 2 May 2022 14:12:35 +0200
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1
Subject: Re: missing feature for "regsub"
Content-Language: en-US
Newsgroups: comp.lang.tcl
References: <t4m08e$3j6$1@dont-email.me> <nnd$50cb211c$6ea16057@c1bbf144b10410cd>
From: nos...@wanadoo.nl (Schelte)
In-Reply-To: <nnd$50cb211c$6ea16057@c1bbf144b10410cd>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Message-ID: <nnd$0620907c$313bb866@19036208da7bc8da>
Organization: KPN B.V.
Path: i2pn2.org!i2pn.org!aioe.org!feeder1.feed.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr1.eu1.usenetexpress.com!94.232.112.244.MISMATCH!feed.abavia.com!abe004.abavia.com!abp002.abavia.com!news.kpn.nl!not-for-mail
Lines: 15
Injection-Date: Mon, 02 May 2022 14:12:35 +0200
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
View all headers
On 02/05/2022 14:04, Schelte wrote:
regsub -command {(\w+)§[ESU]} $string [list apply [list {match prefix} {
     set ch [string index $match end]
     string cat [string map {E enum S struct U union} $ch] " " $prefix
}]] OUTVAR

Of course that can be simplified to:
regsub -command {(\w+)§([ESU])} $string [list apply [list {match prefix ch} {
     string cat [string map {E enum S struct U union} $ch] " " $prefix
}]] OUTVAR


Schelte


Subject: Re: missing feature for "regsub"
From: Harald Oehlmann
Newsgroups: comp.lang.tcl
Organization: A noiseless patient Spider
Date: Mon, 2 May 2022 13:58 UTC
References: 1 2 3
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: wortka...@yahoo.de (Harald Oehlmann)
Newsgroups: comp.lang.tcl
Subject: Re: missing feature for "regsub"
Date: Mon, 2 May 2022 15:58:14 +0200
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <t4oo1m$9hk$1@dont-email.me>
References: <t4m08e$3j6$1@dont-email.me>
<nnd$50cb211c$6ea16057@c1bbf144b10410cd>
<nnd$0620907c$313bb866@19036208da7bc8da>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 2 May 2022 13:58:14 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f4e072ce7b325dd8ce035983160b02fd";
logging-data="9780"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/am6qgJy14PK1O9+zuXE6i"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:Z/iRC/LzDj5itFjvQq7suKH2AX0=
In-Reply-To: <nnd$0620907c$313bb866@19036208da7bc8da>
Content-Language: en-GB
View all headers
Am 02.05.2022 um 14:12 schrieb Schelte:
On 02/05/2022 14:04, Schelte wrote:
regsub -command {(\w+)§[ESU]} $string [list apply [list {match prefix} {
     set ch [string index $match end]
     string cat [string map {E enum S struct U union} $ch] " " $prefix
}]] OUTVAR

Of course that can be simplified to:
regsub -command {(\w+)§([ESU])} $string [list apply [list {match prefix ch} {
     string cat [string map {E enum S struct U union} $ch] " " $prefix
}]] OUTVAR


Schelte

Schelte,
I appreciate your high level answers and deep knowledge of TCL.
I always learn from them!
Thank you for that,
Harald


1
rocksolid light 0.7.2
clearneti2ptor