Recommended way to write a condition with multiple ifs

Viewed 67

I am trying to do alert the user if any 2 of the 4 counter variables is non-zero. What is the best way to code this without a long if else condition like this?

#   Description: report violation if any 2 type of device counts is non-zero (mix of Vt cells)
if { $count_hvt >0 && ($count_lvt >0 || $count_ulvt>0 ||$count_svt >0 ) } {
    return 1
} elseif { $count_lvt >0 && ($count_hvt >0 || $count_ulvt>0 ||$count_svt >0 ) } {
    return 1
} elseif { $count_svt >0 && ($count_hvt >0 || $count_lvt>0 ||$count_ulvt >0 ) } {
    return 1
} elseif { $count_ulvt >0 && ($count_hvt >0 || $count_lvt>0 ||$count_svt >0 ) } {
    return 1
} else {
    #puts "-0- return 0"
    return 0
}
1 Answers

Maybe something like

set counters [list $count_hvt $count_lvt $count_svt $count_ulvt]
if {[llength [lsearch -all -exact -integer -not $counters 0]] >= 2} {
    return 1
}

Basically, filter out the elements equal to zero from a list, and count how many remain.

Related