关于tcl中的命令

时间:2017-04-07 06:31:16

标签: tcl

我想从下一行打印字符:

说:

当文件中存在此变量dum = 183时,则从下一行打印下一个字符。

注意:我使用的是tcl

谢谢,

1 个答案:

答案 0 :(得分:1)

这应该可以帮助您入门。

一次使用一行文件的典型习惯用语是:

1)行读:

set f [open thefile.txt]
while {[gets $f line] >= 0} {
    # work with the line of text in "line"
}
close $f

2)用行拆分阻止读取:

set f [open thefile.txt]
set text [read $f]
close $f
set lines [split [string trim $text] \n]
foreach line $lines {
    # work with the line of text in "line"
}

使用包可以简化:

package require fileutil
::fileutil::foreachLine line thefile.txt {
    # work with the line of text in "line"
}

另一种方法是使用正则表达式进行搜索和提取。这是最差方法,因为它不灵活且很可能在使用中出错。

set f [open thefile.txt]
set text [read $f]
close $f
# this regular expression is an example
if {[regexp {\ydum\y[^\n]*.(.)} $text -> thecharacter]} {
    # the character you wanted should be in "thecharacter"
}

文档: >= (operator)closefileutil (package)foreachgetsifopenpackagereadregexpsetsplitstringwhileSyntax of Tcl regular expressions