Search code examples
comparetcleval

Use comparison operator in a variable in tcl if statement


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" }

Solution

  • 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"
    }