如何执行过程并将宏包含在tcl脚本中

时间:2011-04-05 09:26:26

标签: 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"
       }
    } 
} 

使用它:

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

2 个答案:

答案 0 :(得分:2)

我认为问题在于行末尾没有;,其中包含注释(#)。 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"
       }
    } 
} 

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

答案 1 :(得分:2)

同时检查

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”应该是现有数组的名称。