NAME
switch - Evaluate one of several scripts, depending on a given value
SYNOPSIS
switch ?options? string pattern body ?pattern body ...?
switch ?options? string {pattern body ?pattern body ...?}
DESCRIPTION
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 pat_tern 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. The following options are currently supported:
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.
Below are some examples of switch commands: switch abc a - b {format 1} abc {format 2} default {format 3} will return 2,
switch -regexp aaab {
^a.*b$ -b
{format 1}
a* {format 2}
default {format 3}
}
will return 1, and
switch xyz {
a
-b
{format 1}
a*
{format 2}
default
{format 3}
}
will return 3.
KEYWORDS
switch, match, regular expression