【发布时间】:2022-09-24 04:40:54
【问题描述】:
我有一个以这种形式包含多行的报告文件:
str1 num1 num2 ... numN str2
鉴于 (N) 跨行不同。这些数字代表坐标,所以我需要用花括号将每个点括起来:
{num1 num2} {num3 num4} 等等...
我试过这段代码:
set file_r [open file.rpt r]
set lines [split [read $file_r] \"\\n\"]
close $file_r
foreach line $lines {
set items [split $line]
set str1 [lindex $items 0]
set str2 [lindex $items [expr [llength $items] - 1]]
set box [lrange $items 1 [expr [llength $items] - 2]]
foreach coord $box {
set index [lsearch $box $coord]
set index_rem [expr $index % 2]
if {index_rem == 0} {
set box [lreplace $box $index $index \"{$coord\"]
} else {
set box [lreplace $box $index $index \"$coord}\"]
}
}
puts \"box: $box\"
}
这给了我一个缺少右括号的语法错误。如果我尝试\"\\{$coord\",则会在$box 中输入反斜杠字符。
有什么想法可以克服这个吗?
-
你能得到一个不成对的坐标值列表吗?将它们与
lmap {a b} $coords {list $a $b}配对很容易。 -
我怎么得到它?我不知道点数,因为它因行而异。
标签: tcl