Artifact 8dd35a3e55a252f8a1a9283a7fd323cf1acc401d9d05579d60e46cb001dcec00:
- File doc/markdown/switch.md — part of check-in [bb08aafe0a] at 2025-07-15 19:41:45 on branch tip-700 — some non-change cosmetics to make the ctags implementation in Geany 2.1 partly happy again (user: Torsten, size: 6216) [annotate] [blame] [check-ins using]
CommandName: switch ManualSection: n Version: 8.5 TclPart: Tcl TclDescription: Tcl Built-In Commands Links:
- for(n)
- if(n)
- regexp(n) Keywords:
- switch
- match
- regular expression Copyright:
- Copyright (c) 1993 The Regents of the University of California.
- Copyright (c) 1994-1997 Sun Microsystems, Inc. ---
Name
switch - Evaluate one of several scripts, depending on a given value
Synopsis
::: {.synopsis} ::: [switch]{.cmd} [options=?+ string pattern body= ?+pattern body]{.optdot} [switch]{.cmd} [options=?+ string= +pattern body= ?+pattern body ...]{.optarg} :::
Description
The fBswitchfR command matches its fIstringfR argument against each of the fIpatternfR arguments in order. As soon as it finds a fIpatternfR that matches fIstringfR it evaluates the following fIbodyfR argument by passing it recursively to the Tcl interpreter and returns the result of that evaluation. If the last fIpatternfR argument is fBdefaultfR then it matches anything. If no fIpatternfR argument matches fIstringfR and no default is given, then the fBswitchfR command returns an empty string.
If the initial arguments to fBswitchfR start with fB-fR then they are treated as options unless there are exactly two arguments to fBswitchfR (in which case the first must the fIstringfR and the second must be the fIpatternfR/fIbodyfR list). The following options are currently supported:
-exact : Use exact matching when comparing fIstringfR to a pattern. This is the default.
-glob : When matching fIstringfR to the patterns, use glob-style matching (i.e. the same as implemented by the fBstring matchfR command).
-regexp : When matching fIstringfR to the patterns, use regular expression matching (as described in the fBre_syntaxfR reference page).
-nocase : Causes comparisons to be handled in a case-insensitive manner.
-matchvar fIvarNamefR : This option (only legal when fB-regexpfR is also specified) specifies the name of a variable into which the list of matches found by the regular expression engine will be written. The first element of the list written will be the overall substring of the input string (i.e. the fIstringfR argument to fBswitchfR) matched, the second element of the list will be the substring matched by the first capturing parenthesis in the regular expression that matched, and so on. When a fBdefaultfR branch is taken, the variable will have the empty list written to it. This option may be specified at the same time as the fB-indexvarfR option.
-indexvar fIvarNamefR : This option (only legal when fB-regexpfR is also specified) specifies the name of a variable into which the list of indices referring to matching substrings found by the regular expression engine will be written. The first element of the list written will be a two-element list specifying the index of the start and index of the first character after the end of the overall substring of the input string (i.e. the fIstringfR argument to fBswitchfR) matched, in a similar way to the fB-indicesfR option to the fBregexpfR can obtain. Similarly, the second element of the list refers to the first capturing parenthesis in the regular expression that matched, and so on. When a fBdefaultfR branch is taken, the variable will have the empty list written to it. This option may be specified at the same time as the fB-matchvarfR option.
-|- : Marks the end of options. The argument following this one will be treated as fIstringfR even if it starts with a fB-fR. This is not required when the matching patterns and bodies are grouped together in a single argument.
Two syntaxes are provided for the fIpatternfR and fIbodyfR 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 fIpatternfR 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 fIbodyfR is specified as "-" it means that the fIbodyfR 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 fIbodyfR among several patterns.
Beware of how you place comments in fBswitchfR commands. Comments should only be placed fBinsidefR the execution body of one of the patterns, and not intermingled with the patterns.
Examples
The fBswitchfR command can match against variables and not just literals, as shown here (the result is fI2fR):
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 fI1fR):
switch -glob aaab {
a*b -
b {expr {1}}
a* {expr {2}}
default {expr {3}}
}
Whenever nothing matches, the fBdefaultfR clause (which must be last) is taken. This example has a result of fI3fR:
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 fB-matchvarfR 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"
}
}