1switch(n)                    Tcl Built-In Commands                   switch(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       switch - Evaluate one of several scripts, depending on a given value
9

SYNOPSIS

11       switch ?options? string pattern body ?pattern body ...?
12
13       switch ?options? string {pattern body ?pattern body ...?}
14_________________________________________________________________
15

DESCRIPTION

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                                                                 
40                 Causes  comparisons  to be handled in a case-insensitive man‐ │
41                 ner.                                                          │
42
43       -matchvar varName                                                       │
44                 This option (only legal when -regexp is also specified) spec‐ │
45                 ifies  the  name of a variable into which the list of matches │
46                 found by the regular expression engine will be written.   The │
47                 first  element  of  the list written will be the overall sub‐ │
48                 string of the input  string  (i.e.  the  string  argument  to │
49                 switch)  matched,  the second element of the list will be the │
50                 substring matched by the first capturing parenthesis  in  the │
51                 regular  expression  that matched, and so on.  When a default 
52                 branch is taken, the variable will have the empty list  writ‐ │
53                 ten  to it.  This option may be specified at the same time as │
54                 the -indexvar option.                                         │
55
56       -indexvar varName                                                       │
57                 This option (only legal when -regexp is also specified) spec‐ │
58                 ifies  the  name of a variable into which the list of indices │
59                 referring to matching substrings found by the regular expres‐ │
60                 sion  engine  will be written.  The first element of the list │
61                 written will be a two-element list specifying  the  index  of │
62                 the  start  and index of the first character after the end of │
63                 the overall substring of the input string  (i.e.  the  string
64                 argument to switch) matched, in a similar way to the -indices 
65                 option to the regexp can obtain.  Similarly, the second  ele‐ │
66                 ment of the list refers to the first capturing parenthesis in │
67                 the regular expression that  matched,  and  so  on.   When  a │
68                 default  branch  is  taken,  the variable will have the empty │
69                 list written to it.  This option may be specified at the same │
70                 time as the -matchvar option.
71
72       --        Marks  the  end  of options.  The argument following this one
73                 will be treated as string even if it starts with a  -.   This │
74                 is  not  required  when  the matching patterns and bodies are │
75                 grouped together in a single argument.
76
77       Two syntaxes are provided for the  pattern  and  body  arguments.   The
78       first  uses  a separate argument for each of the patterns and commands;
79       this form is convenient if substitutions are desired  on  some  of  the
80       patterns  or  commands.  The second form places all of the patterns and
81       commands together into a single argument; the argument must have proper
82       list  structure,  with  the elements of the list being the patterns and
83       commands.  The second form makes it easy to construct multi-line switch
84       commands, since the braces around the whole list make it unnecessary to
85       include a backslash at the end of each line.  Since the  pattern  argu‐
86       ments  are in braces in the second form, no command or variable substi‐
87       tutions are performed on them;  this makes the behavior of  the  second
88       form different than the first form in some cases.
89
90       If  a body is specified as “-” it means that the body for the next pat‐
91       tern should also be used as the body for this pattern (if the next pat‐
92       tern  also  has  a body of “-” then the body after that is used, and so
93       on).  This feature makes it possible to share a single body among  sev‐
94       eral patterns.
95
96       Beware  of  how you place comments in switch commands.  Comments should
97       only be placed inside the execution body of one of  the  patterns,  and
98       not intermingled with the patterns.
99

EXAMPLES

101       The  switch  command can match against variables and not just literals,
102       as shown here (the result is 2):
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              switch -glob aaab {
110                 a*b     -
111                 b       {expr {1}}
112                 a*      {expr {2}}
113                 default {expr {3}}
114              }
115
116       Whenever nothing matches, the default clause (which must  be  last)  is
117       taken.  This example has a result of 3:
118              switch xyz {
119                 a -
120                 b {
121                    # Correct Comment Placement
122                    expr {1}
123                 }
124                 c {
125                    expr {2}
126                 }
127                 default {
128                    expr {3}
129                 }
130              }
131
132       When  matching  against  regular  expressions,  information  about what │
133       exactly matched is easily obtained using the -matchvar option:          │
134              switch -regexp -matchvar foo -- $bar {                           │
135                 a(b*)c {                                                      │
136                    puts "Found [string length [lindex $foo 1]] 'b's"          │
137                 }                                                             │
138                 d(e*)f(g*)h {                                                 │
139                    puts "Found [string length [lindex $foo 1]] 'e's and\      │
140                          [string length [lindex $foo 2]] 'g's"                │
141                 }                                                             │
142              }                                                                │
143

SEE ALSO

145       for(n), if(n), regexp(n)
146

KEYWORDS

148       switch, match, regular expression
149
150
151
152Tcl                                   8.5                            switch(n)
Impressum