saitology9 <
[email protected]> wrote:
On 2/3/2023 5:40 AM, snosniv wrote:
I have some time entry data as mins0, secs0, mins1, secs1 etc etc, my script detects if both are unset and raises a warning, but if say mins is set but secs is blank, I set the secs to 0 (and vice-versa):
if {($mins0 == "")} { set mins0 0}
but rather than doing all the mins individually, I'd prefer a loop, but
something like;
for {set i 0} {$i < $max} {incr i} {
{if $mins$i == ""} {set $mins$i 0}
}
obviously won't work.
How do I do get a loop method to work please? (history means I can't really convert to an array easily).
Here is a slightly different solution, especially if you have a large
number of these variables.
Here is how you use it, with an optional third argument which is the
reset value:
test_reset mins0 secs0
test_reset mins1 secs1
...
test_reset mins100 secs100
My suggestion:
for {set i 0} {$i <= 100} {incr i} {
test_reset mins$i secs$i
}
This is the implementation:
proc test_reset {minvar secvar {def 0}} {
upvar \#0 $minvar mins
upvar \#0 $secvar secs
My suggestion:
In your specific case, where all the mins# and secs# variables
are global, this is fine as it is, but more generally, I'd
advise to use level 1 instead of \#0, because then you could
use the helper to also reset local variables in other procedures,
while still being able to reset global variables, by calling
the test_reset from global level. If mins# and secs# are
always global and you want to call test_reset even from
within other procedures and still reset the global ones,
then forget the paragraph, as in that case the "#0" is just right.
One more thing: the # for upvar doesn't really need escaping,
unless you use a syntax-highlighting editor that doesn't know tcl
well - then escaping the # is harmless for tcl, but may be neeeded
to get the coloring right.
In Tcl, # only starts a comment, when it appears where the start
of a command is expected, and that isn't the case after "upvar".
if {[string trim $mins] eq ""} { set mins $def }
if {[string trim $secs] eq ""} { set secs $def }
return
}
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)