expr给出意想不到的结果

时间:2013-11-22 11:28:08

标签: tcl

expr为4个字符(t,n,f,y)提供意外结果。如果你正在做进一步的计算。然后代码就破了。我不明白为什么会这样?

% expr (F)
F

% expr (F)*1
can't use non-numeric string as operand of "*"

% expr (t)
t
% expr (n)
n
% expr (f)
f
% expr (y)
y

这是charcters的文件:t,n,f,y。没有这些字符命名的变量。它应标记未找到的变量或其他一些有效错误。我错过了什么吗?

2 个答案:

答案 0 :(得分:5)

The [expr] conditions of commands such as [if] and [while] expect the expression to evaluate to a boolean, i.e., an integer or one of the following string values:

    true, on, yes
    false, off, no

我相信t,y,f和n是这些的捷径。

答案 1 :(得分:-1)

我认为你期待expr出现问题。

该命令用于评估表达式。它可以对数字进行算术运算,比较字符串或数字,执行一些数学函数等。

你的行

% expr (F)
% expr (t)
% expr (n)
% expr (f)
% expr (y)

所有人都做同样的事情:他们要求对具有更高优先级(大括号)的文字字符串执行 no 操作。所以?没有更多内容,expr返回字符串本身。

% expr (F)*1

但是,您尝试将字符串乘以数字:未定义的操作。实际上,expr给出了一个错误,指出*的一个操作数是非数字字符串(数字F应该代表?)。

使用文字字符串Fy,您可以询问字符串比较。例如,您可以执行以下操作:

% expr F < f
1

(因为在我的编码中,大写字母在小写字母之前)

% expr F == y
0

等等。

所以,expr没有给出任何意想不到的结果,但也许你的期望是错误的。