• Multiple values of a regex to a array

    From Cecil Westerhof@21:1/5 to All on Thu Sep 15 02:12:30 2022
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Cecil Westerhof@21:1/5 to Cecil Westerhof on Thu Sep 15 02:55:23 2022
    Cecil Westerhof <[email protected]> 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to Cecil Westerhof on Thu Sep 15 00:29:04 2022
    Cecil Westerhof <[email protected]> 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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Siri Cruise@21:1/5 to Cecil Westerhof on Wed Sep 14 19:09:19 2022
    In article <[email protected]>,
    Cecil Westerhof <[email protected]> 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Thu Sep 15 05:12:09 2022
    From: Siri Cruise <[email protected]>
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From heinrichmartin@21:1/5 to Cecil Westerhof on Wed Sep 14 22:56:13 2022
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)