Tcl 脚本无法读取 "startreg(1)": 没有这样的变量

Tcl script can't read "startreg(1)": no such variable

我尝试 运行 一个 Tcl 脚本,该脚本从输入文件(定义几何的地方)创建几何文件。该脚本可以 运行 简单地作为 script.tcl 输入文件。 当我 运行 它(在 Mac 和 Linux 上)使用 wish 或 tclsh 命令时,我得到这个错误:

        can't read "startreg(1)": no such variable
        while executing
    "if { $startreg($i)==0 && $stopreg($i)==0 } {
            # All are material 1, change nothing
            } else {
            for {set iz $startz($i)} {$iz<=$stopz($i)} {incr i..."
        invoked from within
    "if [string compare $descrip regions]==0 {
        # Get the mednum, start and stop regions
        seek $fileid $startpos start
        while { [eof $fileid] != 1 } {
           ..."
        (procedure "read_inputfile" line 214)
        invoked from within
    "read_inputfile "
        invoked from within
    "if [file exists $inputfile]==1 {
        read_inputfile 
    } else {
        puts "The file $inputfile doesn't exist!"
        exit
    }"
        (file "~/EGS_Windows/preview3d.tcl" line 580)

任何 help/suggestion 将不胜感激! TA

您显然从未初始化过该变量。

% array set startreg {}
% puts $startreg(1)
can't read "startreg(1)": no such element in array
% unset startreg
% puts $startreg(1)
can't read "startreg(1)": no such variable

startreg是全局变量吗,你忘了在proc中global startreg


我注意到堆栈跟踪中的另一个错误

if [string compare $descrip regions]==0 {

您一定要在条件周围加上大括号,以便在您期望执行测试时执行测试:

if {[string compare $descrip regions]==0} {

这适用于所有 if 表达式,以及所有 expr 一般会话。请参阅此维基页面:http://wiki.tcl.tk/10225

这样的话,if {$descrip eq "regions"}就更清楚了。