英文:
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"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论