tcl - 如何从 tcl 中的文件中提取特定值

标签 tcl

$$ comments             //not needed
$$ comments             /not needed   
$$
$$
$$ comments             //not needed
$$.INPUT a vcc            // needed 
$$.OUTPUT o                //needed 
$$.sdsds                        
$$
$$.sdsds 
$$ 
$$.sdsdsds

Mg1.qna o a vss vss n 0.36 0.03 mult=4 $$nnn //needed
Mg1.qpa o a vcc vcc p 0.36 0.03 mult=6 $$nnn  //needed

行与行之间没有空格 $$

最佳答案

在这种情况下,正则表达式或字符串格式可以提供帮助。但是,目前还不清楚给定示例中文件的格式是什么;很难准确地说出哪些位是有趣的,特定片段的变化范围是什么等等。不过,我们可以采取一些步骤:

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

如前所述,正则表达式也可以用于解析数据行,使用 regexp 而不是 scan 进行匹配,但我不明白格式足以说出要使用什么 RE。

关于tcl - 如何从 tcl 中的文件中提取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5243469/

相关文章:

tcl - 文件不删除 TCL

compilation - tcl_findLibrary 不起作用

multithreading - 在单独的线程中创建的 Tcl Interp 是否会共享任何全局数据?

namespaces - TCL 在应用中使用命名空间

python - 如何从 tkinter 中的绑定(bind)回调中获取回调引用?

tcl - 如何匹配右括号后的冒号

由名称管道驱动的 Tcl/Expect 脚本意外地 block /缓冲区输出

tcl - 在 TCL 命令行中传递参数

c - 如何在TCL中运行预编译的C代码?

escaping - 如何从 Tcl 执行 POSIX shell 转义