I want to pass a list into a procedure but not sure how to. I've looked at some examples of how to do this but the examples are too complicated and I don't understand them. The list and procedure are shown below.
set RS_CheckBoxes [list kl15_cb din1_cb din2_cb din3_cb din4_cb \
dinnc_cb ain1_cb ain2_cb ain3_cb ain4_cb a_cb \
b_cb u_cb v_cb w_cb sin_cb cos_cb th1_cb th2_cb hvil_cb]
tk::button .rs.enter -height 2 -text "Enter" -width 10 -command {x $RS_CheckBoxes}
proc x {$RS_CheckBoxes} {
if {$RS_CheckBoxes} {
puts "ON"
} else {
puts "OFF"
}
}
At present I'm using the below code but want to reduce the amount of lines.
tk::button .relSel.b -height 2 -text "Enter" -width 10 -command {if {$kl15_cb} {
puts "$kl15_cb"
} else {
puts "$kl15_cb"
}
if {$dinnc_cb} {
puts "$dinnc_cb"
} else {
puts "$dinnc_cb"
}
if {$din1_cb} {
puts "$din1_cb"
} else {
puts "$din1_cb"
}
if {......... etc}
............. etc
Your description is not entirely clear. Do you want to pass a list of global variable names into a proc and then print ON or OFF based on their boolean value?
Currently you just seem to be printing the value of the variables in a very complicated way.
if {$dinnc_cb} {
puts "$dinnc_cb"
} else {
puts "$dinnc_cb"
}
is equal to puts $dinnc_cb
unless you want the code to throw an error when the value cannot be interpreted as a boolean.
If my understanding is correct, try this:
proc x {varlist} {
foreach varname $varlist {
upvar #0 $varname var
puts "$varname: [lindex {ON OFF} [expr {!$var}]]"
}
}
The upvar creates a link from the global variable in $varname to the local variable var. You can then use that to check the global variable.
To include a check that the variable is actually set:
proc x {varlist} {
foreach varname $varlist {
upvar #0 $varname var
if {[info exists var]} {
puts "$varname: [lindex {OFF ON} [string is true -strict $var]]"
} else {
puts "$varname: INDETERMINATE"
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.