我不是很有經驗的Tcl程序員,所以我的提議非常直截了當。
從你的問題我想,你逐行讀取文件中的行(我猜使用「獲取」),然後做行(模式匹配)的東西。所以,最直接forwart實施將是這樣的(順便說一下,其中一個問題是你喜歡尾隨「前面的」行的空格和領先的「下一個」行空格做什麼):
;# Note: The code bellow was not tested, and may not run cleanly,
;# but I hope it shows the idea.
;# Like "gets", but concatenates lines, which finish with "\" character with
;# the next one.
proc concatenatingGets {chan} {
set wholeLine ""
set finishedReadingCurentLine no
while {! $finishedReadingCurrentLine } {
set currentLine [gets $chan]
;# more complicated rule can be used here for concatenation
;# of lines
if {[string index $currentLine end] == "\\"} {
;# Decide here what to do with leading and trailing spaces.
;# We just leave them as is (only remove trailing backslash).
;# Note, that Tcl interpreter behaves differently.
append wholeLine " " [string range $currentLine 0 end-1]
} else {
set finishedReadingCurrentLine yes
} ;# if-else strig is to be concatenated
} ;# while ! finishedReadingcurrentLine
} ;# concatenatingGets
;# Now use our tweaked gets:
set f [open "myFileToParse.txt" r]
while {![eof $f]} {
set currentLine [concatenatingGets $f]
;# ... Do pattern matching ot current line, and whatever else needed.
}
close $f
酷。我會給+1顯示習慣用法的表達式,+1用來顯示如何使用Tcl來處理Tcl代碼(不幸的是我只能給出一個+1)。 – 2013-03-25 05:40:16