For some reason, when I use "regexp -inline" and try to capture the output, it fails. Observe:
First, run expect, capture the output - which is the digit string (this works):
expect 1.8> regexp -inline \[0-9]+ [time {sleep 5}]
Result: 5000267
Now, try to capture that result in variable foo:
expect 1.9> set foo [regexp -inline \[0-9]+ [time {sleep 5}]]
Error: wrong # args: should be "regexp ?-option ...? exp string ?matchVar? ?subMatchVar ...?"
Now, try to do the same thing, using lindex, but get same result:
expect 1.11> puts "Time: [lindex [regexp -inline \[0-9]+ [time {sleep 5}]] 0]"
Error: wrong # args: should be "regexp ?-option ...? exp string ?matchVar? ?subMatchVar ...?"
Why?
Why?
Try:
set foo [regexp -inline {[0-9]+} [time {sleep 5}]]
or
set foo [regexp -inline \[0-9\]+ [time {sleep 5}]]
You close the opening "[" with the RE. This does not play any role, if
you don't open a "[".
The point is not the 'set' itself, but the additional pair of [].
In
regexp -inline \[0-9]+ [time {sleep 5}]
the closing "]" is not matched by an opening "[", so TCL just 'uses' it,
as in
set x ]
However, in
set x [regexp -inline \[0-9]+ [time {sleep 5}]]
The "]" after the '9' closes the opening "[" of the 'set' command, so
regexp is actually called with
regexp -inline \[0-9
which is clearly not enough arguments :-). (Note that if it were to
succeed, then 'set' would complain about too many arguments
(the "+ [time {sleep 5}]]")
For some reason, when I use "regexp -inline" and try to capture the output, >it fails. Observe:**************************
First, run expect, capture the output - which is the digit string (this >works):
expect 1.8> regexp -inline \[0-9]+ [time {sleep 5}]
Result: 5000267
Now, try to capture that result in variable foo:
expect 1.9> set foo [regexp -inline \[0-9]+ [time {sleep 5}]]
Error: wrong # args: should be "regexp ?-option ...? exp
string ?matchVar? ?subMatchVar ...?"
Now, try to do the same thing, using lindex, but get same result:
expect 1.11> puts "Time: [lindex [regexp -inline \[0-9]+ [time {sleep 5}]] >0]" Error: wrong # args: should be "regexp ?-option ...? exp
string ?matchVar? ?subMatchVar ...?"
Why?
On 2/8/2024 8:35 AM, Luc wrote:
On Thu, 8 Feb 2024 09:32:06 -0000 (UTC), Kenny McCormack wrote:Based on his working example, the output is the output of [time] and
For some reason, when I use "regexp -inline" and try to capture the
output, it fails. Observe:
First, run expect, capture the output - which is the digit string
I don't know what the output of [time {sleep 5}] is. Can you please
provide some string I can work on? I would like to make a few
tests.
his [sleep 5] must be 5 seconds. I have a wait proc that does that
in ms, so,
% regexp -inline \[0-9\]+ [time {wait 5000}]
5002580
% time {wait 5000}
5012879 microseconds per iteration
%
On Thu, 8 Feb 2024 09:32:06 -0000 (UTC), Kenny McCormack wrote:
For some reason, when I use "regexp -inline" and try to capture the output, >> it fails. Observe:**************************
First, run expect, capture the output - which is the digit string (this
works):
expect 1.8> regexp -inline \[0-9]+ [time {sleep 5}]
Result: 5000267
Now, try to capture that result in variable foo:
expect 1.9> set foo [regexp -inline \[0-9]+ [time {sleep 5}]]
Error: wrong # args: should be "regexp ?-option ...? exp
string ?matchVar? ?subMatchVar ...?"
Now, try to do the same thing, using lindex, but get same result:
expect 1.11> puts "Time: [lindex [regexp -inline \[0-9]+ [time {sleep 5}]] >> 0]" Error: wrong # args: should be "regexp ?-option ...? exp
string ?matchVar? ?subMatchVar ...?"
Why?
I don't know what the output of [time {sleep 5}] is. Can you please
provide some string I can work on? I would like to make a few tests.
et99 <et99@rocketship1.me> wrote:
On 2/8/2024 8:35 AM, Luc wrote:
On Thu, 8 Feb 2024 09:32:06 -0000 (UTC), Kenny McCormack wrote:Based on his working example, the output is the output of [time] and
For some reason, when I use "regexp -inline" and try to capture the
output, it fails. Observe:
First, run expect, capture the output - which is the digit string
I don't know what the output of [time {sleep 5}] is. Can you please
provide some string I can work on? I would like to make a few
tests.
his [sleep 5] must be 5 seconds. I have a wait proc that does that
in ms, so,
% regexp -inline \[0-9\]+ [time {wait 5000}]
5002580
% time {wait 5000}
5012879 microseconds per iteration
%
Kenny's programming Tcl via Expect.
Expect includes a 'sleep' proc as a built in, which works (per the
expect manpage) the same as the Unix/Linux 'sleep' command. The big difference from Tcl's 'after' is Expect's 'sleep' is documented as
allowing Tk events to be processed while it sleeps.
I guess the moral is: Don't be sloppy!
On Thu, 8 Feb 2024 15:38:41 -0000 (UTC), Kenny McCormack wrote:
I guess the moral is: Don't be sloppy!
I no longer remember how but I specifically remember that I once
learned to always enclose regular expressions in curly brackets.
Always. Forget the whole escaping business. Just make sure to
enclose your regular expressions in curly brackets.
And I never had a problem again.
Am 09.02.2024 um 05:39 schrieb Luc:
On Thu, 8 Feb 2024 15:38:41 -0000 (UTC), Kenny McCormack wrote:
I guess the moral is: Don't be sloppy!
I no longer remember how but I specifically remember that I once
learned to always enclose regular expressions in curly brackets.
Always. Forget the whole escaping business. Just make sure to
enclose your regular expressions in curly brackets.
And I never had a problem again.
+1
When I capture the microseconds of a time command, I just use [lindex [time ...]
0] rather than a regex.
I don't know what the output of [time {sleep 5}] is. Can you please
provide some string I can work on? I would like to make a few tests.
In article <uq459p$2etnh$1@dont-email.me>, et99 <et99@rocketship1.me> wrote: ...
When I capture the microseconds of a time command, I just use [lindex [time ...]
0] rather than a regex.
Thanks for that. It never occurred to me that I could extract out the
number via "lindex". Six of one, I suppose.
But I still have to push it through "expr" to convert it to seconds (i.e., divide it by a million).
Kenny McCormack <gazelle@shell.xmission.com> wrote:
In article <uq459p$2etnh$1@dont-email.me>, et99 <et99@rocketship1.me> wrote:
...
When I capture the microseconds of a time command, I just use [lindex [time ...]
0] rather than a regex.
Thanks for that. It never occurred to me that I could extract out the
number via "lindex". Six of one, I suppose.
'lindex' will work here (because the output from 'time' is both
consistent and compatible). But be careful applying lindex to any
arbitrary string, doing so can become the source of 'weird' bugs that
only appear months/years later when "just the right string" happens
through.
I.e.:
$ rlwrap tclsh
% set s "string a b { c d e"
string a b { c d e
% lindex $s 0
unmatched open brace in list
%
But I still have to push it through "expr" to convert it to seconds (i.e., >> divide it by a million).
True, as it returns microseconds. But that is not too hard to
accomodate if you would prefer not having to write out the division at
every usage:
proc time_s {script {count 1}} {
return [expr {[lindex [time $script $count] 0] / 1000000.0}]
}
% time_s {after 500}
0.500647
%
Or, if you really want to get 'fancy':
rename time ::tcl::time
proc time {script {count 1}} {
return [expr {[lindex [::tcl::time $script $count] 0] / 1000000.0}]
}
% time {after 500}
0.500636
%
Although renaming the real time could mess with any package/modules
that also /might/ use [time]. So be careful with this 'fancy' method.
I use a tool called regexbuddy4. It's a commercial product, but well worth >the small price. The best part is that it can write code and it has
templates for 20+ languages, including tcl.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 485 |
Nodes: | 16 (2 / 14) |
Uptime: | 131:37:59 |
Calls: | 9,655 |
Calls today: | 3 |
Files: | 13,707 |
Messages: | 6,166,553 |