gpt4 book ai didi

tcl - 读取换行

转载 作者:行者123 更新时间:2023-12-04 05:05:20 29 4
gpt4 key购买 nike

关闭。这个问题需要更多focused .它目前不接受答案。












想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post .

2年前关闭。



Improve this question




我有一个带有包装行的文件。它恰好是包装多行的 TCL 代码。 (但它可以是任何作为换行规则的东西。)

像:

set long [ some cmd { some long stuff \
more stuff \
even more stuff \
end of cmd} but going on \
end of set ]

我想将其解析为一行,以便我可以对其进行一些模式匹配。

我查看了“读取”命令的文档,但似乎没有这样做。

非常感谢您的帮助。

谢谢,
格特

最佳答案

我不是那么有经验的 Tcl 程序员,所以我的建议很简单。

根据您的问题,我猜您是逐行读取文件(我猜是使用“gets”),然后对该行执行某些操作(模式匹配)。因此,最直接的实现将是这样的(顺便说一句,问题之一是您喜欢如何处理“上一行”的尾随空格和“下”行的前导空格):

;# 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

关于tcl - 读取换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15582257/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com