TCL subst 或 eval 命令在我的情况下不起作用.. [英] TCL subst or eval command is not working in my case ..

查看:53
本文介绍了TCL subst 或 eval 命令在我的情况下不起作用..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

subst or eval command is not working in my case .. 

% proc sum {a b} {
    return [expr $a+$b]
}
%
% set a 1
1
% set b 2
2
% sum $a $b
3

%
% sum {$a} {$b}
can't use non-numeric string as operand of "+"

%
% subst [sum {$a} {$b}]
can't use non-numeric string as operand of "+"   >>>>>>>>> Why i am unable to substitue the value
%

% eval [sum {$a} {$b}]
can't use non-numeric string as operand of "+"    >>>>>>>>> Why i am unable to substitue the value
%

我想知道为什么上述情况对我不起作用.. subst 命令应该执行变量和命令替换.但是为什么我的变量没有替代.

I want to know why the above cases are not working for me .. subst command should do the variable and command substitution . But why my variables are not substituting.

谁能解释一下这是怎么回事?

Can anyone Please Explain what is going on?

推荐答案

首先,您明白为什么$a"不是您可以添加的值?这根本不是一个数字.(我的意思不是 $a,从变量中读取并替换它的指令,我的意思是由 $ 后跟一个 a<组成的字符串/code>.)

First, you do understand why "$a" isn't a value you can add to? It's not a number at all. (I don't mean $a, the instruction to read from a variable and substitute it, I mean the string consisting of a $ followed by an a.)

当你在 Tcl 中把大括号放在东西周围时,它的意思是不要对这个字符串做任何事情;按原样使用它".总是.(有时您将该字符串输入到计算的命令中,但并非总是如此.)当您将方括号括起来时,这意味着将该字符串作为脚本立即进行评估并使用脚本的结果作为要替换的值.总是.

When you put braces round things in Tcl, it means "don't do anything with this string at all; use it as-is". Always. (Sometimes you're feeding that string into a command that evaluates, but not always.) When you put square brackets round things, it means evaluate that string as a script immediately and use the result of the script as the value to substitute. Always.

当你这样做时:

subst [sum {$a} {$b}]

您需要了解对 sum 的调用是在将参数组装到 subst 时完成的.该调用会产生错误,因此对 subst 的调用永远不会发生.与您使用的 eval 表单类似.

You need to understand that the call to sum is done while assembling the arguments to subst. That call produces an error, so the call to subst never happens. Similarly with the eval form you used.

如果我们使用一种不那么令人惊讶的形式:

If we use a somewhat less surprising form:

subst {sum {$a} {$b}}

然后你会得到这个:sum {1} {2}.subst 无法将整个字符串理解为脚本.另一方面,使用:

Then you'll get this out: sum {1} {2}. subst doesn't understand the overall string as a script. On the other hand, with:

eval {sum {$a} {$b}}

在这种情况下,您得到的错误不是来自 eval 本身,而是来自内部对 sum 的调用仍然是错误的.

In this case you get an error not from the eval as such, but rather from the fact that the call to sum inside is still erroneous.

我想你可以这样做:

eval [subst {sum {$a} {$b}}]

但真的不要.必须有一种更简单且不易出错的方法.

But really don't. There's got to be a simpler and less error-prone way.

这篇关于TCL subst 或 eval 命令在我的情况下不起作用..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆