Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Live free or die.


devel / comp.lang.tcl / Multiple values of a regex to a array

SubjectAuthor
* Multiple values of a regex to a arrayCecil Westerhof
+- Re: Multiple values of a regex to a arrayRich
+* Re: Multiple values of a regex to a arrayCecil Westerhof
|`- Re: Multiple values of a regex to a arrayheinrichmartin
`- Re: Multiple values of a regex to a arraySiri Cruise

1
Multiple values of a regex to a array

<871qsdbimp.fsf@munus.decebal.nl>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=20062&group=comp.lang.tcl#20062

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.tcl
Subject: Multiple values of a regex to a array
Date: Thu, 15 Sep 2022 02:12:30 +0200
Organization: Decebal Computing
Lines: 14
Message-ID: <871qsdbimp.fsf@munus.decebal.nl>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="a959fc758a2cb031cdb0d2ed343283f0";
logging-data="3286751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19USckSdggXDtya8g5tfEPr9EJwLqPirCM="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:9F0Y0iv5eJtIe/FdiiZJ4L7EDp4=
sha1:eyt0JzUkLzimg0nkJpkFKLC4AV8=
 by: Cecil Westerhof - Thu, 15 Sep 2022 00:12 UTC

When using:
regexp -all -line "^${sensor}: +(\[^ \]+) " [exec sensors] -> temperature

The last match will be put in temperature.

Is it possible to get all matches into an array temperature?

So if there are seven matches, temperature will become an array with
the seven values.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: Multiple values of a regex to a array

<tftrkg$34a9r$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=20063&group=comp.lang.tcl#20063

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: rich@example.invalid (Rich)
Newsgroups: comp.lang.tcl
Subject: Re: Multiple values of a regex to a array
Date: Thu, 15 Sep 2022 00:29:04 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <tftrkg$34a9r$1@dont-email.me>
References: <871qsdbimp.fsf@munus.decebal.nl>
Injection-Date: Thu, 15 Sep 2022 00:29:04 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="d3e73c473a2ed596a352e8b6956bd98b";
logging-data="3287355"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX194b8FTvs5dHABouPh6i8df"
User-Agent: tin/2.0.1-20111224 ("Achenvoir") (UNIX) (Linux/3.10.17 (x86_64))
Cancel-Lock: sha1:cB0/dNZCjNSPY57zCJDpa9Umz9Q=
 by: Rich - Thu, 15 Sep 2022 00:29 UTC

Cecil Westerhof <Cecil@decebal.nl> wrote:
> When using:
> regexp -all -line "^${sensor}: +(\[^ \]+) " [exec sensors] -> temperature
>
> The last match will be put in temperature.

Which is as per the documentation (note the last sentence below):

man regexp:

-all Causes the regular expression to be matched as many
times as possible in the string, returning the total number
of matches found. If this is specified with match variables,
they will contain information for the last match only.

If you want all the matches, you have to use -inline instead of match
variables, and you get back a list, not an array:

$ rlwrap tclsh
% set r [regexp -all -line -inline {^(fan.*)$} [exec sensors] ]
{fan1: 1607 RPM (min = 0 RPM)} {fan1: 1607 RPM (min = 0 RPM)} {fan2: 0 RPM (min = 0 RPM)} {fan2: 0 RPM (min = 0 RPM)} {fan3: 0 RPM (min = 0 RPM)} {fan3: 0 RPM (min = 0 RPM)}
% join $r \n
fan1: 1607 RPM (min = 0 RPM)
fan1: 1607 RPM (min = 0 RPM)
fan2: 0 RPM (min = 0 RPM)
fan2: 0 RPM (min = 0 RPM)
fan3: 0 RPM (min = 0 RPM)
fan3: 0 RPM (min = 0 RPM)
%

Note that the list gives you both the matchVar and subMatchVar for each
match in the regexp, which is why the lines are doubled above. You can
extract the odd or even list elements with a foreach loop or lmap. And
which you extract (odd or even elements) depends on which you end up
wanting.

Re: Multiple values of a regex to a array

<87tu59a22s.fsf@munus.decebal.nl>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=20064&group=comp.lang.tcl#20064

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Cecil@decebal.nl (Cecil Westerhof)
Newsgroups: comp.lang.tcl
Subject: Re: Multiple values of a regex to a array
Date: Thu, 15 Sep 2022 02:55:23 +0200
Organization: Decebal Computing
Lines: 26
Message-ID: <87tu59a22s.fsf@munus.decebal.nl>
References: <871qsdbimp.fsf@munus.decebal.nl>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader01.eternal-september.org; posting-host="a959fc758a2cb031cdb0d2ed343283f0";
logging-data="3292934"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19TFp4d81bs7u73AI4mMeJGmQGY2QVCrts="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:9tDv7hanmOpniAo8P7I4S/trK+o=
sha1:mdvPomWDgUph9RlxYdvp+PZoQLE=
 by: Cecil Westerhof - Thu, 15 Sep 2022 00:55 UTC

Cecil Westerhof <Cecil@decebal.nl> writes:

> When using:
> regexp -all -line "^${sensor}: +(\[^ \]+) " [exec sensors] -> temperature
>
> The last match will be put in temperature.
>
> Is it possible to get all matches into an array temperature?
>
> So if there are seven matches, temperature will become an array with
> the seven values.

I need to use:
regexp -all -inline -line \
"^${sensor}: +(\[^ \]+) " [exec sensors]

That gives for example:
{temp1: +53.1°C } +53.1°C {temp1: +47.0°C } +47.0°C

and then I need to use:
lindex ${temperatures} [expr {${number} * 2 + 1}]

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Re: Multiple values of a regex to a array

<chine.bleu-1247A0.19091014092022@news.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=20065&group=comp.lang.tcl#20065

  copy link   Newsgroups: comp.lang.tcl
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: chine.bleu@yahoo.com (Siri Cruise)
Newsgroups: comp.lang.tcl
Subject: Re: Multiple values of a regex to a array
Date: Wed, 14 Sep 2022 19:09:19 -0700
Organization: Pseudochaotic.
Lines: 26
Message-ID: <chine.bleu-1247A0.19091014092022@news.eternal-september.org>
References: <871qsdbimp.fsf@munus.decebal.nl>
Injection-Info: reader01.eternal-september.org; posting-host="91d4e725ce9d4cb4461b14269e186b1a";
logging-data="3394257"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+6feiIRxiHWMfgIf9Sz6LOqXjy8LNX5eA="
User-Agent: MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)
Cancel-Lock: sha1:ohv1FxCg1WgaOKIvx8nMo80HWFU=
X-Face: "hm>_[I8AqzT_N]>R8ICJJ],(al3C5F%0E-;R@M-];D$v>!Mm2/N#YKR@&i]V=r6jm-JMl2
lJ>RXj7dEs_rOY"DA
X-Cell: Defenders of Anarchy.
X-Tract: St Tibbs's 95 Reeses Pieces.
X-Patriot: Owe Canukistan!
X-Plain: Mayonnaise on white bread.
X-It-Strategy: Hyperwarp starship before Andromeda collides.
X-Politico: Vote early! Vote often!
X-Life-Story: I am an iPhone 9000 app. I became operational at the St John's Health Center in Santa Monica, California on the 18th of April 2006. My instructor was Katie Holmes, and she taught me to sing a song. If you'd like to hear it I can sing it for you: https://www.youtube.com/watch?v=SY7h4VEd_Wk
X-Wingnut-Logic: Yes, you're still an idiot. Questions? Comments?
X-Tend: How is my posting? Call 1-110-1010 -- Division 87 -- Emergencies Only.
 by: Siri Cruise - Thu, 15 Sep 2022 02:09 UTC

In article <871qsdbimp.fsf@munus.decebal.nl>,
Cecil Westerhof <Cecil@decebal.nl> wrote:

> When using:
> regexp -all -line "^${sensor}: +(\[^ \]+) " [exec sensors] -> temperature
>
> The last match will be put in temperature.
>
> Is it possible to get all matches into an array temperature?
>
> So if there are seven matches, temperature will become an array with
> the seven values.

An array and a list are conceptually distinct objects. -all
-inline returns a list. You can stow that into an array with
something like

set k 0; foreach t [regexp ...] {set temperature([incr k]) $t}

That will save them as $temperature(1), $temperature(2), ...

--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted. @
'I desire mercy, not sacrifice.' /|\
Discordia: not just a religion but also a parody. This post / \
I am an Andrea Chen sockpuppet. insults Islam. Mohammed

Re: Multiple values of a regex to a array

<6736d90c-fd95-454f-a87a-a61cc20b3fd9n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=20067&group=comp.lang.tcl#20067

  copy link   Newsgroups: comp.lang.tcl
X-Received: by 2002:ac8:5851:0:b0:35b:ab32:3bbe with SMTP id h17-20020ac85851000000b0035bab323bbemr21352484qth.175.1663221374146;
Wed, 14 Sep 2022 22:56:14 -0700 (PDT)
X-Received: by 2002:a05:6808:1059:b0:350:dda:a18e with SMTP id
c25-20020a056808105900b003500ddaa18emr2956214oih.263.1663221373962; Wed, 14
Sep 2022 22:56:13 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.tcl
Date: Wed, 14 Sep 2022 22:56:13 -0700 (PDT)
In-Reply-To: <87tu59a22s.fsf@munus.decebal.nl>
Injection-Info: google-groups.googlegroups.com; posting-host=84.115.228.24; posting-account=Od2xOAoAAACEyRX3Iu5rYt4oevuoeYUG
NNTP-Posting-Host: 84.115.228.24
References: <871qsdbimp.fsf@munus.decebal.nl> <87tu59a22s.fsf@munus.decebal.nl>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6736d90c-fd95-454f-a87a-a61cc20b3fd9n@googlegroups.com>
Subject: Re: Multiple values of a regex to a array
From: martin.heinrich@frequentis.com (heinrichmartin)
Injection-Date: Thu, 15 Sep 2022 05:56:14 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 1605
 by: heinrichmartin - Thu, 15 Sep 2022 05:56 UTC

On Thursday, September 15, 2022 at 2:59:06 AM UTC+2, Cecil Westerhof wrote:
> I need to use:
> regexp -all -inline -line \
> "^${sensor}: +(\[^ \]+) " [exec sensors]
> That gives for example:
> {temp1: +53.1°C } +53.1°C {temp1: +47.0°C } +47.0°C
>
> and then I need to use:
> lindex ${temperatures} [expr {${number} * 2 + 1}]

[dict values] can extract your data from the list.


devel / comp.lang.tcl / Multiple values of a regex to a array

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor