在Tcl的if语句中使用比较运算符。

huangapple go评论86阅读模式
英文:

Use comparison operator in a variable in tcl if statement

问题

I would like to use the comparison operator like == >= and so in a variable as shown in the following example. I can not get it to work as intended, and I bet I use the eval statement wrong here. Can anyone help me out?

set distance 4
set rule "=="
set tolerance 4

if { ![eval $distance $rule $tolerance] } { puts "distance is equal to tolerance" } else { puts "bailout" }

英文:

I would like to use the comparison operator like == >= and so in in a variable as shown in the following example. I can not get it to work as intended a i bet I use the eval statement wrong here.
Can anyone help me out?

set distance 4
set rule "=="
set tolerance 4

if { ![ eval [ $distance $rule $tolerance ] ] } { puts "distance is equal to tolerance" } else { puts "bailout" }

答案1

得分: 1

你可以使用 "expr" 代替 "eval":

set distance 4
set rule "=="
set tolerance 4

if { ! [expr $distance $rule $tolerance] } {
    puts "distance is equal to tolerance"
} else {
    puts "bailout"
}
英文:

You may use "expr" instead of "eval":

set distance 4
set rule "=="
set tolerance 4

if { ! [expr $distance $rule $tolerance] } {
    puts "distance is equal to tolerance"
} else {
    puts "bailout"
}

huangapple
  • 本文由 发表于 2023年5月24日 21:55:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76324312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定