Tcl 为什么expr是;i==i";以“失败”;无效的裸字“;?
(一) (二) 为什么我在第2步中遇到这个错误Tcl 为什么expr是;i==i";以“失败”;无效的裸字“;?,tcl,Tcl,(一) (二) 为什么我在第2步中遇到这个错误 % expr "i==i" invalid bareword "i" in expression "i==i"; should be "$i" or "{i}" or "i(...)" or ... 如果{“i”==“i”}这是if条件下的工作 在这里,我发现like-expr命令只计算整数,不比较字符串,但在“if”条件下,所有内容(整数和字符串)都在计算 这里的情况如何?答案在expr手册页中 1) % if {"i" == "i"} {
% expr "i==i"
invalid bareword "i"
in expression "i==i";
should be "$i" or "{i}" or "i(...)" or ...
如果{“i”==“i”}这是if条件下的工作
在这里,我发现like-expr命令只计算整数,不比较字符串,但在“if”条件下,所有内容(整数和字符串)都在计算
这里的情况如何?答案在
expr
手册页中
1) % if {"i" == "i"} {
puts "hai"
}
hai
2) % if {i == "i"} {
puts "hai"
}
invalid bareword "i"
in expression "i == "i"";
should be "$i" or "{i}" or "i(...)" or ...
因此,expr
可以比较字符串,但必须将它们括在双引号或大括号中,这取决于是否要执行替换
因此,在示例2中,必须使用
Operands may be specified in any of the following ways:
...
[4] As a string enclosed in double-quotes. The expression parser
will perform backslash, variable, and command substitutions on
the information between the quotes, and use the resulting value
as the operand
[5] As a string enclosed in braces. The characters between the open
brace and matching close brace will be used as the operand with‐
out any substitutions.
...
或
最好使用字符串比较操作数:
% expr {{i} == {i}}
确保字符串的内容未转换为数值。答案在
expr
手册页中
1) % if {"i" == "i"} {
puts "hai"
}
hai
2) % if {i == "i"} {
puts "hai"
}
invalid bareword "i"
in expression "i == "i"";
should be "$i" or "{i}" or "i(...)" or ...
因此,expr
可以比较字符串,但必须将它们括在双引号或大括号中,这取决于是否要执行替换
因此,在示例2中,必须使用
Operands may be specified in any of the following ways:
...
[4] As a string enclosed in double-quotes. The expression parser
will perform backslash, variable, and command substitutions on
the information between the quotes, and use the resulting value
as the operand
[5] As a string enclosed in braces. The characters between the open
brace and matching close brace will be used as the operand with‐
out any substitutions.
...
或
最好使用字符串比较操作数:
% expr {{i} == {i}}
确保字符串的内容未转换为数值。在Tcl 8.4中
你可以用
% expr {"i" eq "i"}
% expr {{i} eq {i}}
或
这两种语法都可以使用。在Tcl 8.4中
你可以用
% expr {"i" eq "i"}
% expr {{i} eq {i}}
或
这两种语法都可以使用。即使我尝试了相同的语法,但仍然会出现相同的错误。%expr“i”=“i”表达式“i==i”中的无效单字“i”;应该是“$i”或“{i}”或“i(…)”或…%@user2746819您可以执行
expr{“i”==“i”}
@Jerry是对的:我错过了所附的{…}
。我正在编辑answer@Jerry,相反;-)if
和其他条件命令使用的引擎与expr
使用的引擎相同。@Marco这是对的,只是更准确地说if
和expr
都调用相同的基础引擎。(就像和的一样,即使我尝试了同样的方法,但仍然得到了同样的错误expr“i”=“i”表达式“i==i”中的无效单字“i”;应该是“$i”或“{i}”或“i(…)”或…%@user2746819您可以执行expr{“i”==“i”}
@Jerry是对的:我错过了所附的{…}
。我正在编辑answer@Jerry,相反;-)if
和其他条件命令使用的引擎与expr
使用的引擎相同。@Marco这是对的,只是更准确地说if
和expr
都调用相同的基础引擎。(就像一样,而和对于)这里只有第一个更好。建议使用expr{“i”eq“i”}
。第二个是。这里只有第一个更好。建议使用expr{“i”eq“i”}
。第二个是。