12.5.39    switch

Description:   Execute a command if  Evaluate a string,  compare to one or more string patterns, execute command if it matches  a given string pattern

Syntax:         switch -options string pattern body pattern body… default  body

                   switch -options string {pattern body pattern body ... }


Argument:     options, string,  string patterns, commands if pattern match, default if no match

Returns:        command.  Blank string if no match and no default

See Also:       if, then,  for, foreach



#example 1

set z ZERO

set x2 [GETVAL text5]

switch $x2 \

  "$z"        {set txt2 {text5 matches z}; set txt1 0 } \

  "ONE"       {set txt2 {text5 is ONE}; set txt1 1 } \

  "TWO"       {set txt2 {text5 is TWO}; set txt1 2 } \

  "THREE"     {set txt2 {text5 is THREE}; set txt1 3 } \

  "default"   {set txt2 {text5 has NO MATCH};}


SETVAL text50=$txt2

SETVAL analog3=$txt1

# If text5="ZERO" then text50="text 5 matches z" and analog3=0

# If text5="ONE" then text50="text 5 is ONE" and analog3=1

# If text5="TWO" then text50="text 5 is TWO" and analog3=2

# If text5="THREE" then text50="text 5 is THREE" and analog3=3

# else text50=" text5 has NO MATCH"


#example 2

catch {

switch $x "ONE" {SETVAL text6=1} "TWO" {SETVAL text6=2} "default" {SETVAL text6=none}




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. The following options are 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 (i.e. the same as implemented by the regexp command).

--                     (double dashes) Mark the end of options. The argument following this one will be treated as string even if it starts with a - (dash).

Two syntax 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 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 commands, since the braces around the whole list make it unnecessary to include a backslash at the end of each line. Since the pattern arguments are in braces in the second form, no command or variable substitutions are performed on them; this makes the behavior of the second form different than the first form in 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 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.