Textual Comparison - switch

The switch command allows you to choose one of several options in your code. It is similar to switch in C, except that it is more flexible, because you can switch on strings, instead of just integers. The string will be compared to a set of patterns, and when a pattern matches the string, the code associated with that pattern will be evaluated.

It's a good idea to use the switch command when you want to match a variable against several possible values, and don't want to do a long series of if... elseif ... elseif statements.

The syntax of the command is:

- or -

String is the string that you wish to test, and pattern1, pattern2, etc are the patterns that the string will be compared to. If string matches a pattern, then the code within the body associated with that pattern will be executed. The return value of the body will be returned as the return value of the switch statement. Only one pattern will be matched.

If the last pattern argument is the string default, that pattern will match any string. This guarantees that some set of code will be executed no matter what the contents of string are.

If there is no default argument, and none of the patterns match string, then the switch command will return an empty string.

If you use the brace version of this command, there will be no substitutions done on the patterns. The body of the command, however, will be parsed and evaluated just like any other command, so there will be a pass of substitutions done on that, just as will be done in the first syntax. The advantage of the second form is that you can write multiple line commands more readably with the brackets.

Note that you can use braces to group the body argument when using the switch or if commands. This is because these commands pass their body argument to the Tcl interpreter for evaluation. This evaluation includes a pass of substitutions just as it does for code not within a command body argument.


Example

set x "ONE"
set y 1
set z ONE

# This is probably the easiest and cleanest form of the command
# to remember:
switch $x {
    "$z" {
        set y1 [expr {$y+1}]
        puts "MATCH \$z. $y + $z is $y1"
    }
    ONE {
        set y1 [expr {$y+1}]
        puts "MATCH ONE. $y + one is $y1"
    }
    TWO {
        set y1 [expr {$y+2}]
        puts "MATCH TWO. $y + two is $y1"
    }
    THREE {
        set y1 [expr {$y+3}]
        puts "MATCH THREE. $y + three is $y1"
    }
    default {
        puts "$x is NOT A MATCH"
    }
}


switch $x "$z" {
    set y1 [expr {$y+1}]
    puts "MATCH \$z. $y + $z is $y1"
} ONE {
    set y1 [expr {$y+1}]
    puts "MATCH ONE. $y + one is $y1"
} TWO {
    set y1 [expr {$y+2}]
    puts "MATCH TWO. $y + two is $y1"
} THREE {
    set y1 [expr {$y+3}]
    puts "MATCH THREE. $y + three is $y1"
} default {
    puts "$x does not match any of these choices"
}


switch $x "ONE" "puts ONE=1" "TWO" "puts TWO=2" "default" "puts NO_MATCH"

switch $x \
"ONE"     "puts ONE=1"  \
"TWO"     "puts TWO=2" \
"default"     "puts NO_MATCH";