home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Other Langs / Tickle-4.0 (tcl) / library / help / tcl / control / switch < prev    next >
Encoding:
Text File  |  1993-10-26  |  3.5 KB  |  86 lines  |  [TEXT/$Tcl]

  1.  
  2.           switch ?options? string pattern body ?pattern body ...?
  3.           switch ?options? string {pattern body ?pattern body ...?}
  4.  
  5.  
  6.      DESCRIPTION
  7.           The switch command matches its string argument against  each
  8.           of  the  pattern  arguments in order.  As soon as it finds a
  9.           pattern that matches string it evaluates the following  body
  10.           argument  by  passing  it recursively to the Tcl interpreter
  11.           and returns the result of that evaluation.  If the last pat-
  12.           tern  argument  is  default then it matches anything.  If no
  13.           pattern argument matches string and  no  default  is  given,
  14.           then the switch command returns an empty string.
  15.  
  16.           If the initial arguments to switch start with  -  then  they
  17.           are treated as options.  The following options are currently
  18.           supported:
  19.  
  20.           -exact    Use exact matching when comparing string to a pat-
  21.                     tern.  This is the default.
  22.  
  23.           -glob     When matching string to the  patterns,  use  glob-
  24.                     style  matching  (i.e.  the same as implemented by
  25.                     the string match command).
  26.  
  27.           -regexp   When matching string to the patterns, use  regular
  28.                     expression  matching (i.e. the same as implemented
  29.                     by the regexp command).
  30.  
  31.           --        Marks the end of options.  The argument  following
  32.                     this  one  will  be  treated  as string even if it
  33.                     starts with a -.
  34.  
  35.           Two syntaxes are provided for the  pattern  and  body  argu-
  36.           ments.   The  first uses a separate argument for each of the
  37.           patterns and commands; this form is convenient if  substitu-
  38.           tions  are desired on some of the patterns or commands.  The
  39.           second form places all of the patterns and commands together
  40.           into  a  single argument; the argument must have proper list
  41.           structure, with the elements of the list being the  patterns
  42.           and  commands.   The  second form makes it easy to construct
  43.           multi-line switch commands,  since  the  braces  around  the
  44.           whole list make it unnecessary to include a backslash at the
  45.           end of each line.  Since the pattern arguments are in braces
  46.           in the second form, no command or variable substitutions are
  47.           performed on them;  this makes the behavior  of  the  second
  48.           form different than the first form in some cases.
  49.  
  50.           If a body is specified as ``-'' it means that the  body  for
  51.           the  next  pattern  should also be used as the body for this
  52.           pattern (if the next pattern also has a body of  ``-''  then
  53.           the body after that is used, and so on).  This feature makes
  54.           it possible to share a single body among several patterns.
  55.  
  56.           Below are some examples of switch commands:
  57.  
  58.                switch abc a - b {format 1} abc {format 2} default {format 3}
  59.  
  60.           will return 2,
  61.  
  62.                switch -regexp aaab {
  63.                  ^a.*b$ -
  64.                  b {format 1}
  65.                  a* {format 2}
  66.                  default {format 3}
  67.                }
  68.           will return 1, and
  69.  
  70.                switch xyz {
  71.                  a
  72.                    -
  73.                  b
  74.                    {format 1}
  75.                  a*
  76.                    {format 2}
  77.                  default
  78.                    {format 3}
  79.                }
  80.  
  81.           will return 3.
  82.  
  83.  
  84.      KEYWORDS
  85.           switch, match, regular expression
  86.