Re: extra character error



In article <pySQg.5150$7I1.2043@xxxxxxxxxxxxxxxxxxxxxxxxxx>,
Bryan Oakley <oakley@xxxxxxxxxxxxxxxxxxxx> wrote:
.
.
.
I think that's more in the spirit of what you're trying to do. As I
understand it, you're wanting to store conditions in variables, and use
those stored conditions in an if statement.

It's hard for us to give a definitive answer because you're asking a
somewhat complex question with not enough details. For example, where
you have "-1<0", is that really an expression you're wanting to use or
is your real world data more complex? Can these initial expressions have
variable names or references to tcl procedures? And so on...

When it all boils down, it's a matter of quoting. The if statement is
defined as "if <expression> ...", and <expression> be be expressed any
way you want. That's the beauty of Tcl. It means you can do things like:

if $variable ...
if [someCommand] ...
if {some hard-coded expression} ...

Tcl doesn't care. It takes the first argument *after it does it's own
normal round of substitution* and passes it to the if command. The if
command then does a *second* round of substitution. So, your job is to
figure out how to give the if command something reasonable after tcl's
normal pass at substitution.

Traditionally (and not coincidentally, the most efficient and
recommended way) to do this is to enclose a hard-coded expression in
curly braces, knowing that expr will do it's own round of substitution
on what it is given. The curly braces guarantee that what the expression
parser sees is exactly what you typed.

99.99% of the time, avoiding this double substitution is a Good Thing.
You have stumbled into that other 0.01% of the time when you want double
substitution. Namely, you have an expression in a variable rather than
hard-coded into your code.

So, think like the interpreter for a second and try to see what will
actually be handed off to the if command after tcl does its normal round
of substitution. Be mindful of the effects of curly braces and quotes up
to and including the call to the if command. At each step ask yourself
"do I want variables substituted now?".

Along with all that Bryan quite perfectly expresses, I feel obliged
to point out that it's unusual to *need* this much work. I'll give
details: it's crucial to "think like the interpreter" and follow
the other advice Bryan gave. However, it's very, very unusual for
conventional end-user applications, especially those written by
newcomers, to need the trickiness covered here of evaluation-time
conditional variables or so on.

So, along with Bryan's counsel, I offer this: if it feels to you
that you're working hard to get the quoting in Tcl right <URL:
http://wiki.tcl.tk/1726 >, ask for help. There's probably a simpler
way to go about your larger goal.
.



Relevant Pages

  • Re: extra character error
    ... That's the beauty of Tcl. ... normal round of substitution* and passes it to the if command. ...
    (comp.lang.tcl)
  • Re: extra character error
    ... That's the beauty of Tcl. ... normal round of substitution* and passes it to the if command. ...
    (comp.lang.tcl)
  • Re: extra character error
    ... That's the beauty of Tcl. ... It takes the first argument *after it does it's own normal round of substitution* and passes it to the if command. ... think like the interpreter for a second and try to see what will actually be handed off to the if command after tcl does its normal round of substitution. ...
    (comp.lang.tcl)
  • Re: Dangers of web apps written in Tcl
    ... As 'man tcl' says in the "Command ... I don't know enough scripting languages with sufficient depth ... that the presence of command substitution in Tcl opens no security ...
    (comp.lang.tcl)
  • Re: Large binary data manipulation
    ... looked at binary data. ... It was a revelation that the string command could be used to handle ... If left unbraced, the byte-code compiler ... > substitution could break an expression. ...
    (comp.lang.tcl)