other languages
conflicting packages
switch(3tcl) | Tcl Built-In Commands | switch(3tcl) |
NAME¶
switch - Evaluate one of several scripts, depending on a given valueSYNOPSIS¶
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 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:- -exact
- Use exact matching when comparing string to a pattern. This is the default.
- -glob
- When matching string to the patterns, use glob-style matching (i.e. the same as implemented by the string match command).
- -regexp
- When matching string to the patterns, use regular expression matching (as described in the re_syntax reference page).
- -nocase
- Causes comparisons to be handled in a case-insensitive manner.
- -matchvar varName
- This option (only legal when -regexp 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 string argument to switch) 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 default branch is taken, the variable will have the empty list written to it. This option may be specified at the same time as the -indexvar option.
- -indexvar varName
- This option (only legal when -regexp 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 string argument to switch) matched, in a similar way to the -indices option to the regexp 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 default branch is taken, the variable will have the empty list written to it. This option may be specified at the same time as the -matchvar option.
- --
- Marks the end of options. The argument following this one will be treated as string even if it starts with a -. This is not required when the matching patterns and bodies are grouped together in a single argument.
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}}
switch -glob aaab { a*b - b {expr {1}} a* {expr {2}} default {expr {3}} }
switch xyz { a - b { # Correct Comment Placement expr {1} } c { expr {2} } default { expr {3} } }
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(3tcl), if(3tcl), regexp(3tcl)KEYWORDS¶
switch, match, regular expression8.5 | Tcl |