Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 28 |
Nodes: | 6 (0 / 6) |
Uptime: | 48:14:34 |
Calls: | 422 |
Files: | 1,024 |
Messages: | 90,421 |
On 3/21/2025 6:56 PM, Rich wrote:
When the parser is parsing the command, the third "word" of the first
for loop is:
"\$i $op \$q"
Due to rule four, variable substitution and backslash substution is
performed on this word, because it is double quoted.
I read "double quoted" a few times - I think you meant "double-quote'd".
So since it double-quoted, the substitutions occur as you described. And whatever results from that becomes a "static" argument/parameter to the for-loop. Is this right?
I recall warnings against using double quotes with loops since the substitution occurs once.
I guess I could benefit from this if you explain why the for-loop keeps re-evaluating a double-quoted string.
On 3/21/2025 10:25 AM, saito wrote:
Consider:
set q 10
se op "<"
for {set i 1} {$i $op $q} {incr i} {puts $i}
The answer is yes if you put it in an [expr] :-)
The for command is defined as always running expr on the middle
argument. Whether you get a loop that looks up variable contents by
that expr call to make the check dynamic, or a loop that runs expr on
the exact same static values for each iteration, depends upon what you
pass to the command. That depends upon what you write that is parsed
by the Tcl parser.
right.FWIW, I remember when I first started to use [expr {$a eq "b"}] if took me *AGES* to get the syntax right. To be fair to [expr], its error message (if you omit the double-quotes) is pretty specific, but I still found it a struggle to get the syntax
The lesson I took from this was to stop writing
set i foo
and to start writing
set i "foo"
but the temptation to save a couple of keystrokes is still strong :-)
Consider:
set q 10
set op "<"
for {set i 1} {$i $op $q} {incr i} {puts $i}
On 3/21/2025 3:34 PM, Emiliano wrote:
On Fri, 21 Mar 2025 10:25:17 -0400
Several. Here are two
for {set i 1} "\$i $op \$q" {incr i} {puts $i}
or
for {set i 1} [format {$i %s $q} $op] {incr i} {puts $i}
Nice solutions, especially the former one. I am surprised that the double-quotes work here, with repeated evaluations. Nice approach.
And [format] looks suspicious in its use here :-)