I rediscovered a handy option in the switch statement of the Tcl language today. Consider the following (rather long-winded I admit) snippet that carries out certain actions for various similar type of values of the variable x
.
switch $x {
typeA -
typeB -
typeC -
typeD { ;# Do something for all 4 types }
otherA -
otherB { ;# Do something else for all 2 others }
default { puts "Invalid Option: $x" }
}
Using the -glob
parameter on the switch command in conjunction with the regular expression-like tweaks to the case conditions allows the entire construct to be simplified as follows:
switch -glob $x {
type[ABCD] { ;# Do something for all 4 types }
other[AB]Â { ;# Do something else for all others }
default { puts "Invalid Option: $x" }
}