Tcl lindex 和 expr:非数字失败

Tcl lindex and expr: non-numeric failure

如果我只放在下面就可以了。

"\{[lindex ($columns) 1] - 30.3]"

如果我像下面这样写,那是行不通的。不知道为什么?

"\{[lindex ($columns) 1] - 30.3] [expr [lindex ($columns) 2] -30.3] \}"

我的脚本如下:

foreach line $lines {
    set columns [split $line " "]
    puts "\{[lindex ($columns) 1] - 30.3] [expr [lindex ($columns) 2] -30.3] \}"
}

问题是您写的是 ($columns) 而不是 $columns,这是在您传递给 lindex 的列表中串联括号。在这种情况下,我怀疑该列表具有三个简单元素(例如 1 2 3)并且串联的结果是 (1 2 3)。索引 1 的中间元素仍然可以,但末尾的元素(索引 2)现在是 3),并且是非数字的。

整件事都是语法错误。正确的写法如下:

puts "\{[expr {[lindex $columns 1] - 30.3}] [expr {[lindex $columns 2] -30.3}] \}"

但是,在这种情况下,这样写可能会更清楚一些:

lassign [split $line " "] c1 c2 c3
puts [format "{%f %f}" [expr {$c2 - 30.3}] [expr {$c3 - 30.3}]]