Multiple values of a regex to a array

34 views
Skip to first unread message

Cecil Westerhof

unread,
Sep 14, 2022, 8:14:07 PMSep 14
to
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

Rich

unread,
Sep 14, 2022, 8:29:08 PMSep 14
to
Cecil Westerhof <Ce...@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.

Cecil Westerhof

unread,
Sep 14, 2022, 8:59:06 PMSep 14
to
Cecil Westerhof <Ce...@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}]

Siri Cruise

unread,
Sep 14, 2022, 10:09:41 PMSep 14
to
In article <871qsdb...@munus.decebal.nl>,
Cecil Westerhof <Ce...@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

clt.to...@dfgh.net

unread,
Sep 15, 2022, 1:12:20 AMSep 15
to
>From: Siri Cruise <chine...@yahoo.com>
>Date: Thu Sep 15 02:09:19 GMT 2022
>Subject: Multiple values of a regex to a array
scan is very tolerant of extra stuff.
I'm guessing what the data looks like, but something similar might work

# sample data
set ss {temp1: +45.5 degC temp2: +56.3 degC temp3: +34.7 degC}

set n -5
while {0 <= [set n [string first temp $ss $n+5]]} {
array set data [scan [string range $ss $n end] {temp%d: %f}]
}

The results are in the data array indexed by the sensor number.

Dave B

heinrichmartin

unread,
Sep 15, 2022, 1:56:16 AMSep 15
to
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.
Reply all
Reply to author
Forward
0 new messages