开发者

How to execute procedures and can macros be included in a tcl script

I came across the following program in a profile today....... its a good one written with great intellect.... however I was not able to run it.......

proc parseFileContents {contents infoVar} { 
    upvar 1 $infoVar inf 
    set lineNum 0 
    foreach line [split $contents "\开发者_JAVA百科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"
       }
    } 
} 

Using it:

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


Also check that the

if {[string match {$*} $line}

is missing a closing bracket

if {[string match {$*} $line]}

The final code should be like that.

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 still fails put a copy of your content file. Keep in mind that "infoVar" should be the name of an existing array.


I believe the problem is that there is no ; at the end of the lines with comments (#) in them. Tcl uses either newline or semi-column as a line seperator, and comments are a line by themselves. try using this instead:

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"
       }
    } 
} 

Note that I did not test this new code, only fixed the syntax errors...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜