如何在tcl脚本中执行过程和宏

如何在tcl脚本中执行过程和宏,tcl,Tcl,我今天在个人资料中看到了以下节目。。。。。。。这本书写得很好,很有智慧。。。。然而,我无法运行它 proc parseFileContents {contents infoVar} { upvar 1 $infoVar inf set lineNum 0 foreach line [split $contents "\n"] { incr lineNum # Skip comment lines (?) if {[s

我今天在个人资料中看到了以下节目。。。。。。。这本书写得很好,很有智慧。。。。然而,我无法运行它

proc parseFileContents {contents infoVar} { 
    upvar 1 $infoVar inf 
    set lineNum 0 
    foreach line [split $contents "\n"] {  
       incr lineNum         # Skip comment lines (?) 
       if {[string match {$*} $line} continue         # Skip blank lines 
       if {[string trim $line] eq ""} continue        # Parse a "real" line    
       if {[scan $line "%s%s%s%s%s%s%f%f%s%s" a b c name d e value f g h] == 10} {             
           set inf($name) $value
       } else { 
           # Oh dear, didn't work!
           puts "warning: did not understand line $lineNum\n$line"
       }
    } 
} 
使用它:

parseFileContents $theContentsOfTheFile data 
puts "Keys: [array names data]" 
puts "VSS: $data(vss)" puts "VCC: $data(vcc)"

我相信问题在于没有
位于行的末尾,其中包含注释(
#
)。Tcl使用换行符或半列作为行分隔符,注释本身就是一行。请尝试使用此选项:

proc parseFileContents {contents infoVar} { 
    upvar 1 $infoVar inf 
    set lineNum 0 
    foreach line [split $contents "\n"] {  
       incr lineNum;         # Skip comment lines (?) 
       if {[string match {$*} $line]} continue;         # Skip blank lines 
       if {[string trim $line] eq ""} continue;        # Parse a "real" line    
       if {[scan $line "%s%s%s%s%s%s%f%f%s%s" a b c name d e value f g h] == 10} {             
           set inf($name) $value
       } else { 
           # Oh dear, didn't work!
           puts "warning: did not understand line $lineNum\n$line"
       }
    } 
} 

请注意,我没有测试这段新代码,只是修复了语法错误…

我认为问题在于没有
位于行的末尾,其中包含注释(
#
)。Tcl使用换行符或半列作为行分隔符,注释本身就是一行。请尝试使用此选项:

proc parseFileContents {contents infoVar} { 
    upvar 1 $infoVar inf 
    set lineNum 0 
    foreach line [split $contents "\n"] {  
       incr lineNum;         # Skip comment lines (?) 
       if {[string match {$*} $line]} continue;         # Skip blank lines 
       if {[string trim $line] eq ""} continue;        # Parse a "real" line    
       if {[scan $line "%s%s%s%s%s%s%f%f%s%s" a b c name d e value f g h] == 10} {             
           set inf($name) $value
       } else { 
           # Oh dear, didn't work!
           puts "warning: did not understand line $lineNum\n$line"
       }
    } 
} 
请注意,我没有测试这段新代码,只是修复了语法错误…

还要检查

if {[string match {$*} $line}
缺少一个结束括号

if {[string match {$*} $line]}
最终的代码应该是这样的

proc parseFileContents {contents infoVar} { 
    upvar 1 $infoVar inf 
    set lineNum 0 
    foreach line [split $contents "\n"] {  
       incr lineNum;         # Skip comment lines (?) 
       if {[string match {$*} $line]} continue;         # Skip blank lines 
       if {[string trim $line] eq ""} continue;        # Parse a "real" line    
       if {[scan $line "%s%s%s%s%s%s%f%f%s%s" a b c name d e value f g h] == 10} {             
           set inf($name) $value
       } else { 
           # Oh dear, didn't work!
           puts "warning: did not understand line $lineNum\n$line"
       }
    } 
}
如果仍然失败,请放置内容文件的副本。请记住,“infoVar”应该是现有数组的名称。

还要检查

if {[string match {$*} $line}
缺少一个结束括号

if {[string match {$*} $line]}
最终的代码应该是这样的

proc parseFileContents {contents infoVar} { 
    upvar 1 $infoVar inf 
    set lineNum 0 
    foreach line [split $contents "\n"] {  
       incr lineNum;         # Skip comment lines (?) 
       if {[string match {$*} $line]} continue;         # Skip blank lines 
       if {[string trim $line] eq ""} continue;        # Parse a "real" line    
       if {[scan $line "%s%s%s%s%s%s%f%f%s%s" a b c name d e value f g h] == 10} {             
           set inf($name) $value
       } else { 
           # Oh dear, didn't work!
           puts "warning: did not understand line $lineNum\n$line"
       }
    } 
}

如果仍然失败,请放置内容文件的副本。请记住,“infoVar”应该是现有阵列的名称。

什么是“我无法运行它”呢?有任何错误信息吗?
$theContentsOfTheFile
的内容是什么?我发现很难执行它“我无法运行它”的确切含义是什么?有任何错误信息吗?
$theContentsOfTheFile
的内容是什么?我发现很难执行它注意到glob模式
$*
与空行不匹配,它与以美元符号开头的行匹配。所有注释都被移动。我只是太懒了,没法把它们修好。每一条评论都指向下一条评论line@Carlos,我的评论实际上是针对询问者的。@Nazeeb,请在案例中添加一个示例,说明如何使用变量定义调用proc。另外,“put$::errorInfo”的结果也很难帮助您。@Nazeeb,如果您这样做,vcc可能没问题,因为您正在发送变量名,但是vss应该是$vss,因为您要发送变量的内容。请注意,glob模式
$*
与空行不匹配,它匹配以美元符号开头的一行。所有注释都会移位。我只是太懒了,没法把它们修好。每一条评论都指向下一条评论line@Carlos,我的评论实际上是针对询问者的。@Nazeeb,请在案例中添加一个示例,说明如何使用变量定义调用proc。另外,“put$::errorInfo”的结果也很难帮助您。@Nazeeb,如果您这样做,vcc可能没问题,因为您正在发送变量名,但是vss应该是$vss,因为您要发送变量的内容。。。。除了
字符串匹配
行中缺少的括号外。。。除了
字符串匹配
行中缺少的括号外