Sujet : Re: can this work?
De : rich (at) *nospam* example.invalid (Rich)
Groupes : comp.lang.tclDate : 21. Mar 2025, 23:56:01
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vrkqq1$2hm9c$1@dont-email.me>
References : 1 2 3
User-Agent : tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
saito <
saitology9@gmail.com> wrote:
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 :-)
Remember the rules of Tcl [1]:
[4]
If the first character of a word is double-quote (``"'') then the
word is terminated by the next double-quote character. If
semi-colons, close brackets, or white space characters (including
newlines) appear between the quotes then they are treated as
ordinary characters and included in the word. Command
substitution, variable substitution, and backslash substitution are
performed on the characters between the quotes as described below.
The double-quotes are not retained as part of the word.
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.
So $op is replaced with <, and both \$ are replaced with $
Resulting in |$i < $q| ( | to show edges of the "word").
Then the 'for' command is called, with it's second parameter being
$i < $q
The for loop then calls expr on this parameter, and expr looks up the
value of the i and q variables, and tests them for less than. If the
result is true, the loop continues, if the result is false, the loop
stops.
[1]
https://www.tcl-lang.org/man/tcl8.2.3/TclCmd/Tcl.htm