Previous
While Loop
While Loop
Next
Procedures
Procedures
Tcl/Tk Tutorial - Create GUI using Tk with Tcl Language
Switch Loop
Switch Loop
The Control Flow Loops
Switch Loop
Evaluate one of several scripts, depending on a given value.
Syntax:switch ?options? string { pattern body ?pattern body ...? }
I like this syntax better...
switch ?options? { string } {
pattern { body }
?pattern { body }
...?
}
?options? can be any of the below -
-exact | Use exact matching when comparing string to a pattern. This is the default. |
-glob | When matching string to the patterns, use glob-style matching (i.e. the same as implemented by the string match command). |
-regexp | When matching string to the patterns, use regular expression matching |
#The marriage status script...
#Change the status please
set marital_status "After"
label .thesis -text "Marriage is a three ring circus..."
switch $marital_status {
"Before" { set ring "Engagement" }
"During" { set ring "Wedding" }
default { set ring "suffe -" }
}
label .proof -text "$marital_status Marriage: $ring ring"
pack .thesis
pack .proof
Run this script and give the 'marital_status' to "Before", "During" and "After" and see the different results.