switch(3tcl) | Tcl Built-In Commands | switch(3tcl) |
switch - Evaluate one of several scripts, depending on a given value
switch ?options? string pattern body ?pattern body ...?
switch ?options? string {pattern body ?pattern body ...?}
The switch command matches its string argument against each of the pattern arguments in order. As soon as it finds a pattern that matches string it evaluates the following body argument by passing it recursively to the Tcl interpreter and returns the result of that evaluation. If the last pattern argument is default then it matches anything. If no pattern argument matches string and no default is given, then the switch command returns an empty string.
If the initial arguments to switch start with - then they are treated as options unless there are exactly two arguments to switch (in which case the first must the string and the second must be the pattern/body list). The following options are currently supported:
Two syntaxes are provided for the pattern and body arguments. The first uses a separate argument for each of the patterns and commands; this form is convenient if substitutions are desired on some of the patterns or commands. The second form places all of the patterns and commands together into a single argument; the argument must have proper list structure, with the elements of the list being the patterns and commands. The second form makes it easy to construct multi-line switch commands, since the braces around the whole list make it unnecessary to include a backslash at the end of each line. Since the pattern arguments are in braces in the second form, no command or variable substitutions are performed on them; this makes the behavior of the second form different than the first form in some cases.
If a body is specified as “-” it means that the body for the next pattern should also be used as the body for this pattern (if the next pattern also has a body of “-” then the body after that is used, and so on). This feature makes it possible to share a single body among several patterns.
Beware of how you place comments in switch commands. Comments should only be placed inside the execution body of one of the patterns, and not intermingled with the patterns.
The switch command can match against variables and not just literals, as shown here (the result is 2):
set foo "abc" switch abc a - b {expr {1}} $foo {expr {2}} default {expr {3}}
Using glob matching and the fall-through body is an alternative to writing regular expressions with alternations, as can be seen here (this returns 1):
switch -glob aaab {
a*b -
b {expr {1}}
a* {expr {2}}
default {expr {3}} }
Whenever nothing matches, the default clause (which must be last) is taken. This example has a result of 3:
switch xyz {
a -
b {
# Correct Comment Placement
expr {1}
}
c {
expr {2}
}
default {
expr {3}
} }
When matching against regular expressions, information about what exactly matched is easily obtained using the -matchvar option:
switch -regexp -matchvar foo -- $bar {
a(b*)c {
puts "Found [string length [lindex $foo 1]] 'b's"
}
d(e*)f(g*)h {
puts "Found [string length [lindex $foo 1]] 'e's and\
[string length [lindex $foo 2]] 'g's"
} }
switch, match, regular expression
8.5 | Tcl |