【问题标题】:TCL parsing issueTCL解析问题
【发布时间】:2015-12-22 23:42:28
【问题描述】:

下面是我的 $file_path 的内容

if **{**[info exists ABC] && $ABC == "xyz"} **{**
    # constraints
    # TODO:
    echo "do something"
 }

我希望打印该文件的内容,就像它在 dofile.tcl 中一样。但不知何故,下面的代码开始和结束的左括号“{”以粗体突出显示被省略了。

结果:

if [info exists ABC] && $ABC == "xyz"} **#Notice the missing { both in begin and end**
    # constraints
    # TODO:
    echo "do something"
 }

代码:

set fp [open "$file_path" r]
                set file_data [read $fp]
                close $fp
                echo "D1 : file data is $file_data" >> abc.log
                set data [split $file_data "\n"]
                foreach line $data {
                    echo "D2: Line is $line" >> abc.log
                    if { ! [regexp {^\#} $line] } {
                       echo "$line" >> dofile.tcl
                          }
                }

D1 根据需要使用“{”打印整个数据,而 D2 省略“{”

【问题讨论】:

  • 无法复制。尝试创建一个minimal reproducible example
  • 最后一行**exec** echo "$line" >> dofile.tcl解决了大括号缺失的问题。

标签: regex parsing split tcl quotes


【解决方案1】:

尝试使用 puts 代替 echo,如下所示:

set lf [open "abc.log" w]
set tf [open "dofile.tcl" w]
set fp [open "$file_path" r]
set file_data [read $fp]
close $fp
puts $lf "D1 : file data is $file_data"
set data [split $file_data "\n"]
foreach line $data {
    puts $lf "D2: Line is $line"
    if { ! [regexp {^\#} $line] } {
       puts $tf "$line"
     }
}
close $lf
close $tf

我的输出如下所示:

if **{**[info exists ABC] && $ABC == "xyz"} **{**
    # constraints
    # TODO:
    echo "do something"
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 2019-07-16
    • 2015-09-30
    相关资源
    最近更新 更多