Consider:
set q 10
se op "<"
for {set i 1} {$i $op $q} {incr i} {puts $i}
Consider:
set q 10
set op "<"
for {set i 1} {$i $op $q} {incr i} {puts $i}
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}
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 :-)
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.
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"
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.
On 3/22/2025 8:11 AM, Rich wrote:
--snip--
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.
Actually, the manual for the [for] command does not say it runs [expr], rather, it only says:
Then it repeatedly evaluates test as an expression;
And the command [expr] is not mentioned at all. Also, in the page with
the 12 rules, it never defines the word expression.
The [if] command, however, does mention the use of [expr]:
The if command evaluates expr1 as an expression (in the same way that expr evaluates its argument).
I suppose one has to get deep into the weeds and fully understand the algorithm of [expr] to be able to parse it all. There, [expr] does
define an expression.
One item that took me forever to understand is why in most commands,
words such as in these 3,
set foo bar
set foo {bar}
set foo "bar"
the 2 types of quotes don't change the result here from the unquoted version. But in [expr] and therefore also in the first argument to [if]
and the second to [for] a string has to be quoted in one of the 2 ways.
So that,
if {$foo eq "bar"} ..
if {$foo eq {bar}} ..
is ok, but
if {$foo eq bar} ..
is not ok. And the reason is that,
expr {$foo eq bar}
also produces an error since operands in [expr] are not the same as tcl words. Here, [expr] complains about a bare word - something I've also
not seen defined.
Anyway, there's always something to learn here :)
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 right.
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 :-)
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,029 |
Nodes: | 10 (0 / 10) |
Uptime: | 156:00:41 |
Calls: | 13,333 |
Calls today: | 3 |
Files: | 186,574 |
D/L today: |
4,921 files (1,307M bytes) |
Messages: | 3,355,923 |