1switch(n) Tcl Built-In Commands switch(n)
2
3
4
5______________________________________________________________________________
6
8 switch - Evaluate one of several scripts, depending on a given value
9
11 switch ?options? string pattern body ?pattern body ...?
12
13 switch ?options? string {pattern body ?pattern body ...?}
14______________________________________________________________________________
15
17 The switch command matches its string argument against each of the pat‐
18 tern arguments in order. As soon as it finds a pattern that matches
19 string it evaluates the following body argument by passing it recur‐
20 sively to the Tcl interpreter and returns the result of that evalua‐
21 tion. If the last pattern argument is default then it matches any‐
22 thing. If no pattern argument matches string and no default is given,
23 then the switch command returns an empty string.
24
25 If the initial arguments to switch start with - then they are treated
26 as options unless there are exactly two arguments to switch (in which
27 case the first must the string and the second must be the pattern/body
28 list). The following options are currently supported:
29
30 -exact Use exact matching when comparing string to a pattern. This
31 is the default.
32
33 -glob When matching string to the patterns, use glob-style matching
34 (i.e. the same as implemented by the string match command).
35
36 -regexp When matching string to the patterns, use regular expression
37 matching (as described in the re_syntax reference page).
38
39 -nocase Causes comparisons to be handled in a case-insensitive man‐
40 ner.
41
42 -matchvar varName
43 This option (only legal when -regexp is also specified) spec‐
44 ifies the name of a variable into which the list of matches
45 found by the regular expression engine will be written. The
46 first element of the list written will be the overall sub‐
47 string of the input string (i.e. the string argument to
48 switch) matched, the second element of the list will be the
49 substring matched by the first capturing parenthesis in the
50 regular expression that matched, and so on. When a default
51 branch is taken, the variable will have the empty list writ‐
52 ten to it. This option may be specified at the same time as
53 the -indexvar option.
54
55 -indexvar varName
56 This option (only legal when -regexp is also specified) spec‐
57 ifies the name of a variable into which the list of indices
58 referring to matching substrings found by the regular expres‐
59 sion engine will be written. The first element of the list
60 written will be a two-element list specifying the index of
61 the start and index of the first character after the end of
62 the overall substring of the input string (i.e. the string
63 argument to switch) matched, in a similar way to the -indices
64 option to the regexp can obtain. Similarly, the second ele‐
65 ment of the list refers to the first capturing parenthesis in
66 the regular expression that matched, and so on. When a
67 default branch is taken, the variable will have the empty
68 list written to it. This option may be specified at the same
69 time as the -matchvar option.
70
71 -- Marks the end of options. The argument following this one
72 will be treated as string even if it starts with a -. This
73 is not required when the matching patterns and bodies are
74 grouped together in a single argument.
75
76 Two syntaxes are provided for the pattern and body arguments. The
77 first uses a separate argument for each of the patterns and commands;
78 this form is convenient if substitutions are desired on some of the
79 patterns or commands. The second form places all of the patterns and
80 commands together into a single argument; the argument must have proper
81 list structure, with the elements of the list being the patterns and
82 commands. The second form makes it easy to construct multi-line switch
83 commands, since the braces around the whole list make it unnecessary to
84 include a backslash at the end of each line. Since the pattern argu‐
85 ments are in braces in the second form, no command or variable substi‐
86 tutions are performed on them; this makes the behavior of the second
87 form different than the first form in some cases.
88
89 If a body is specified as “-” it means that the body for the next pat‐
90 tern should also be used as the body for this pattern (if the next pat‐
91 tern also has a body of “-” then the body after that is used, and so
92 on). This feature makes it possible to share a single body among sev‐
93 eral patterns.
94
95 Beware of how you place comments in switch commands. Comments should
96 only be placed inside the execution body of one of the patterns, and
97 not intermingled with the patterns.
98
100 The switch command can match against variables and not just literals,
101 as shown here (the result is 2):
102
103 set foo "abc"
104 switch abc a - b {expr {1}} $foo {expr {2}} default {expr {3}}
105
106 Using glob matching and the fall-through body is an alternative to
107 writing regular expressions with alternations, as can be seen here
108 (this returns 1):
109
110 switch -glob aaab {
111 a*b -
112 b {expr {1}}
113 a* {expr {2}}
114 default {expr {3}}
115 }
116
117 Whenever nothing matches, the default clause (which must be last) is
118 taken. This example has a result of 3:
119
120 switch xyz {
121 a -
122 b {
123 # Correct Comment Placement
124 expr {1}
125 }
126 c {
127 expr {2}
128 }
129 default {
130 expr {3}
131 }
132 }
133
134 When matching against regular expressions, information about what
135 exactly matched is easily obtained using the -matchvar option:
136
137 switch -regexp -matchvar foo -- $bar {
138 a(b*)c {
139 puts "Found [string length [lindex $foo 1]] 'b's"
140 }
141 d(e*)f(g*)h {
142 puts "Found [string length [lindex $foo 1]] 'e's and\
143 [string length [lindex $foo 2]] 'g's"
144 }
145 }
146
148 for(n), if(n), regexp(n)
149
151 switch, match, regular expression
152
153
154
155Tcl 8.5 switch(n)