无效的命令名称“”错误

时间:2017-04-26 13:01:55

标签: tcl

我用了一段时间来提取向量的tcl脚本突然停止工作,我不确定为什么。此外,错误似乎也没有意义。

我正在运行的代码是:

for {set resd 501} {$resd < 502} {incr resd 1} {
set basefile1 "CCvector$resd"

set workdir [pwd]
set nf [molinfo top get numframes]

set fil [open $basefile1.dat w]

for {set frame 0} {$frame < $nf} {incr frame 1} {
    animate goto $frame
    display update ui
    set c1 [atomselect top "name C1 and resid $resd and resname G130"]
    set c3 [atomselect top "name C3 and resid $resd and resname G130"]
    set c1c [$c1 get {x y z} ]
    set c3c [$c3 get {x y z} ]
    set c1c3x [expr [$c3 get x]-[$c1 get x]]
    set c1c3y [expr [$c3 get y]-[$c1 get y]]
    set c1c3z [expr [$c3 get z]-[$c1 get z]]
    set st [expr $frame]
    puts $fil [list $st $c1c3x $c1c3y $c1c3z ]
    $c3 delete
    $c1 delete 
}
close $fil

我收到的原始错误是“在 @ 上缺少操作数”,但是我将部分代码替换为:

for {set frame 0} {$frame < $nf} {incr frame 1} {
    animate goto $frame
    display update ui
    set c1 [atomselect top "name C1 and resid $resd and resname G130"]
    set c3 [atomselect top "name C3 and resid $resd and resname G130"]
    set c1x [$c1 get x]
    set c3x [$c3 get x]
    set c1c3x [expr [$c3x - $c1x]]
    set c1y [$c1 get y]
    set c3y [$c3 get y]
    set c1c3y [expr [$c3y - $c1y]]
    set c1z [$c1 get z]
    set c3z [$c3 get z]
    set c1c3z [expr [$c3z - $c1z]]
    set st [expr $frame]
    puts $fil [list $st $c1c3x $c1c3y $c1c3z ]
    $c3 delete
    $c1 delete 
}
close $fil

现在正在收到“无效的命令名称”“”错误。我哪里错了?

附加信息:我正在使用VMD运行此操作,以从加载的gromacs轨迹中提取坐标。

1 个答案:

答案 0 :(得分:3)

在:

set c1c3z [expr [$c3z - $c1z]]

您尝试使用$c3z-的内容作为参数运行$c1z命令(并将其返回值作为参数传递给expr)。

要等同于以前版本的代码,它将是:

set c1c3z [expr $c3z - $c1z]

但是,由于$c3z似乎是空的(所以不是数字),您可能会遇到更多问题。

此处,$c3z$c1z很可能为空,这意味着expr会对" - "表达式进行评估,您将会看到:

$ tclsh <<< 'expr " - "'
missing operand at _@_
in expression " - _@_"

如果正如Donal在评论中所建议的那样,你写的是:

set c1c3z [expr {$c3z - $c1z}]

相反,文字$c3z - $c1z会传递给exprexpr会在尝试评估时提供更有用的错误消息:

$ tclsh <<< 'set a ""; expr {$a - $a}'
can't use empty string as operand of "-"

expr TCL man page会为您提供更多信息,说明为什么通常会将{} - 附带的表达式传递给它。

相关问题