Reputation: 13
I'm trying to evaluate certain expression. I have "pqr" && "xyz" in command. on evaluating the command, it gives an error: extra characters after close-quote. I think tcl cant able to parse && after double quote. If this is the reason then how should i have to deal with double quote and &&?
Upvotes: 0
Views: 10301
Reputation: 13252
You're not giving us enough information.
A wild guess is that you were writing something like this:
expr "pqr"&& "xyz"
which does give the error message "extra characters after close-quote". This is because the interpreter tries to parse the command according to Tcl language rules, and one of those rules is that a word that starts with a double quote must end with a matching double quote. In this case, there are two &
characters following the matching double quote.
Now,
expr "pqr" && "xyz"
(with a space between the double quote and the ampersand) is no good either. This is because the interpreter will remove any characters that have syntactic function as it prepares the arguments for the command. This means that the argument expr
gets is the string pqr && xyz
. When the expr
command executes, it tries to interpret its argument as a string in a special expression language that isn't Tcl. In particular, unlike in Tcl strings that aren't boolean values or the names of variables of functions must always be enclosed in braces or double quotes, like this: "pqr" && "xyz"
. So how do you get that? You always* brace the argument to expr
, that's how.
expr {"pqr" && "xyz"}
means that expr
gets the legal string "pqr" && "xyz"
.
But the string "pqr" && "xyz"
is still not valid, since the &&
(logical and) operation isn't defined for strings other than strings that are equal to the string representation of boolean values, such as expr {"true" && "false"}
So, again we're stuck, because what you seem to be trying to do makes no sense. If you show us what you're doing we might be able to help you.
*) except when you shouldn't. Rare, expert level.
Documentation: expr, Mathematical operators as Tcl commands, Summary of Tcl language syntax
Brace your expressions
The expr command (and by extension, the commands for
, if
, and while
, which use the same mechanism to evaluate their conditions) interprets an expression string that is constructed from its arguments. Note that the language of the expression string isn't Tcl, but specific to the expr
command's expression evaluator: the languages share many syntactic forms, but are fundamentally different with infix, operator-based structure for the expr
language and prefix, command-based structure for Tcl.
Letting the Tcl interpreter evaluate the arguments before passing them to expr
can lead to
for
, while
) getting constant-valued condition arguments, leading to infinite loops.expr
can selectively suppress some of them.Therefore, it is almost always better to provide the expression string as a braced (escaped) string, which will not be evaluated by the Tcl interpreter, only the expr
interpreter.
Note that while unbraced arguments to expr
are allowed to be a invalid expression string as long as the argument evaluation transforms them into a valid one, braced expressions must be valid as they are (e.g. variable or command substitutions must be simple operands and not operators or complex expressions).
Another benefit from using braced expression strings is that the byte compiler usually can generate more efficient code (5 - 10x faster) from them.
Upvotes: 2