switch(n)
π 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 π¦
okay bestie, here's the vibe: switch takes its string argument and matches it against each of the pattern arguments, in order. The second it finds a pattern that matches string, it evaluates the body argument that comes right after it β by passing that body recursively to the Tcl interpreter β and returns the result of that evaluation. π«Ά
If the last pattern argument is default, it matches literally anything. And if no pattern matches string and there's no default given? Then switch just hands you back an empty string. no drama, just vibes π
If the initial arguments to switch start with -, they get treated as options β unless there are exactly two arguments to switch (in which case the first must be the string and the second must be the pattern/body list). Here are the options that are currently supported, and yes we're going through all of them:
β¨ OPTIONS β¨
-exactβ use exact matching when comparingstringto apattern. This is the default, so if you do nothing, this is what you get. π-globβ when matchingstringto the patterns, use glob-style matching (i.e. the same as implemented by thestring matchcommand). π-regexpβ when matchingstringto the patterns, use regular expression matching (as described in there_syntaxreference page). π§-nocaseβ causes comparisons to be handled in a case-insensitive manner. π₯-matchvarvarName β this option (only legal when-regexpis 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. thestringargument toswitch) 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 adefaultbranch is taken, the variable will have the empty list written to it. This option may be specified at the same time as the-indexvaroption. π-indexvarvarName β this option (only legal when-regexpis 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. thestringargument toswitch) matched, in a similar way to the-indicesoption to theregexpcan obtain. Similarly, the second element of the list refers to the first capturing parenthesis in the regular expression that matched, and so on. When adefaultbranch is taken, the variable will have the empty list written to it. This option may be specified at the same time as the-matchvaroption. π--β marks the end of options. The argument following this one will be treated asstringeven if it starts with a-. This is not required when the matching patterns and bodies are grouped together in a single argument. π
πΈ The Two Syntaxes πΈ
There are two syntaxes provided for the pattern and body arguments, and you get to pick:
- The first one 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. That argument must have proper list structure, with the elements of the list being the patterns and commands. This form makes it easy to construct multi-line
switchcommands, since the braces around the whole list make it unnecessary to include a backslash at the end of each line. And since the pattern arguments are in braces in the second form, no command or variable substitutions are performed on them β which makes the behavior of the second form different than the first form in some cases. π¦
π Fall-Through Bodies π
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 (and 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. iconic behavior tbh β¨
β οΈ 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. π
π₯ EXAMPLES π₯
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"
}
}
π SEE ALSO π
for(n), if(n), regexp(n) β go read those next bestie, they're all part of the family π«Ά
β KEYWORDS β
switch, match, regular expression
okay that's the whole page, you ate it, periodt π
β¨ this was Tcl 8.5, manual section (n) β the built-in commands chapter, iykyk ππ mwah, go write some slay conditionals ππ¦πΈ
π¦ where this page comes from
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1997 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms