switch(1,n)(n) Tcl Built-In Commands switch(1,n)(n) NAME switch(1,n) - Evaluate one of several scripts, depending on a given value SYNOPSIS switch(1,n) ?options? string(3,n) pattern body ?pattern body ...? switch(1,n) ?options? string(3,n) {pattern body ?pattern body ...?} DESCRIPTION The switch(1,n) command matches its string(3,n) argument against each of the pat- tern arguments in(1,8) order. As soon as it finds a pattern that matches string(3,n) it evaluates the following body argument by passing it recur- sively to the Tcl interpreter and returns the result of that evalua- tion. If the last pattern argument is default then it matches any- thing. If no pattern argument matches string(3,n) and no default is given, then the switch(1,n) command returns an empty string. If the initial arguments to switch(1,n) start with - then they are treated as options. The following options are currently supported: -exact Use exact matching when comparing string(3,n) to a pattern. This is the default. -glob When matching string(3,n) to the patterns, use glob-style matching (i.e. the same as implemented by the string(3,n) match command). -regexp When matching string(3,n) to the patterns, use regular expression matching (as described in(1,8) the re_syntax reference page). -- Marks the end of options. The argument following this one will be treated as string(3,n) even if(3,n) it starts with a -. Two syntaxes are provided for the pattern and body arguments. The first uses a separate argument for each of the patterns and commands; this form is convenient if(3,n) 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; the argument must have proper list structure, with the elements of the list being the patterns and commands. The second form makes it easy to construct multi-line switch(1,n) commands, since the braces around the whole list make it unnecessary to include a backslash at the end of each line. Since the pattern argu- ments are in(1,8) braces in(1,8) the second form, no command or variable substi- tutions are performed on them; this makes the behavior of the second form different than the first form in(1,8) some cases. 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 (if(3,n) 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. Beware of how you place comments in(1,8) switch(1,n) commands. Comments should only be placed inside the execution body of one of the patterns, and not intermingled with the patterns. Below are some examples of switch(1,n) commands: switch(1,n) abc a - b {format 1} abc {format 2} default {format 3} will return 2, switch(1,n) -regexp aaab { ^a.*b$ - b {format 1} a* {format 2} default {for- mat 3} } will return 1, and switch(1,n) xyz { a - b { # Correct Comment Placement format 1 } a* {format 2} default {format 3} } will return 3. SEE ALSO for(n), if(3,n)(n), regexp(3,n)(n) KEYWORDS switch(1,n), match, regular expression Tcl 7.0 switch(1,n)(n)