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
16

DESCRIPTION

18       The switch command matches its string argument against each of the pat‐
19       tern arguments in order.  As soon as it finds a  pattern  that  matches
20       string  it  evaluates  the following body argument by passing it recur‐
21       sively to the Tcl interpreter and returns the result  of  that  evalua‐
22       tion.   If  the  last  pattern argument is default then it matches any‐
23       thing.  If no pattern argument matches string and no default is  given,
24       then the switch command returns an empty string.
25
26       If  the  initial arguments to switch start with - then they are treated
27       as options.  The following options are currently supported:
28
29       -exact    Use exact matching when comparing string to a pattern.   This
30                 is the default.
31
32       -glob     When matching string to the patterns, use glob-style matching
33                 (i.e. the same as implemented by the string match command).
34
35       -regexp   When matching string to the patterns, use regular  expression
36                 matching (as described in the re_syntax reference page).
37
38       --        Marks  the  end  of options.  The argument following this one
39                 will be treated as string even if it starts with a -.
40
41       Two syntaxes are provided for the  pattern  and  body  arguments.   The
42       first  uses  a separate argument for each of the patterns and commands;
43       this form is convenient if substitutions are desired  on  some  of  the
44       patterns  or  commands.  The second form places all of the patterns and
45       commands together into a single argument; the argument must have proper
46       list  structure,  with  the elements of the list being the patterns and
47       commands.  The second form makes it easy to construct multi-line switch
48       commands, since the braces around the whole list make it unnecessary to
49       include a backslash at the end of each line.  Since the  pattern  argu‐
50       ments  are in braces in the second form, no command or variable substi‐
51       tutions are performed on them;  this makes the behavior of  the  second
52       form different than the first form in some cases.
53
54       If  a  body  is  specified as ``-'' it means that the body for the next
55       pattern should also be used as the body for this pattern (if  the  next
56       pattern  also has a body of ``-'' then the body after that is used, and
57       so on).  This feature makes it possible to share a  single  body  among
58       several patterns.
59
60       Beware  of  how you place comments in switch commands.  Comments should
61       only be placed inside the execution body of one of  the  patterns,  and
62       not intermingled with the patterns.
63

EXAMPLES

65       The  switch  command can match against variables and not just literals,
66       as shown here (the result is 2):
67              set foo "abc"
68              switch abc a - b {expr 1} $foo {expr 2} default {expr 3}
69
70       Using glob matching and the fall-through  body  is  an  alternative  to
71       writing  regular  expressions  with  alternations,  as can be seen here
72       (this returns 1):
73              switch -glob aaab {
74                 a*b     -
75                 b       {expr 1}
76                 a*      {expr 2}
77                 default {expr 3}
78              }
79
80       Whenever nothing matches, the default clause (which must  be  last)  is
81       taken.  This example has a result of 3:
82              switch xyz {
83                 a  -
84                 b {
85                    # Correct Comment Placement
86                    expr 1
87                 }
88                 c {
89                    expr 2
90                 }
91                 default {
92                    expr 3
93                 }
94              }
95
96

SEE ALSO

98       for(n), if(n), regexp(n)
99
100

KEYWORDS

102       switch, match, regular expression
103
104
105
106Tcl                                   7.0                            switch(n)
Impressum