Sujet : Re: can this work?
De : rich (at) *nospam* example.invalid (Rich)
Groupes : comp.lang.tclDate : 22. Mar 2025, 16:11:37
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vrmjv9$6fer$1@dont-email.me>
References : 1 2 3 4 5
User-Agent : tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
saito <
saitology9@gmail.com> wrote:
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?
The argument is always a "static" argument. With double quotes (the
name of ASCII character \ux22) it undergoes a first round of quoting
when the parser is first parsing the command (before the for loop
executes) and then is parsed by "expr" on each iteration of the loop.
I recall warnings against using double quotes with loops since the
substitution occurs once.
That depends upon what the initial string in the source is.
This:
set i 0 ; set op < ; set q 5
for {} "$i $op $q" {} {...}
Will have the parser, in the inital parse of the command, replace i, op
and q with "0 < 5" and pass that final string to the for loop, and on
each iterationn of the loop, the loop runs expr on "0 < 5" and yes,
this is a "static" value and the loop runs forever (or until you kill
it).
However, that above was not the original example.
What was in the original example was:
set i 0 ; set op < ; set q 5
for {} "\$i $op \$q" {} {...}
The back slashes in "\$i < \$q" are of critical importance. Their
presence make this **not** a static loop situation. The reason why is
during the first level parse, before the for loop is even run, only one
variable is substituted (the op variable). What gets passed to the for
command by the parser is "$i < $q", which, when passed to expr, will
look up the current values of i and q to use for the comparison,
resulting in a loop that terminates (assuming the i and q variables are
updated by the loop iterations).
I guess I could benefit from this if you explain why the for-loop keeps
re-evaluating a double-quoted string.
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.