case

This Tcl command is obsolete and is supported only for backward compatibility. At some point in the future it may be removed entirely. You should use the switch command instead.

Fair enough, but you can't erase this command from history. In fact the command is valid in current Tcl!

Until recently there was a link to http://purl.org/tcl/home/man/tcl8.4/TclCmd/case.htm but that link is dead now. It would be fine if someone could restore this piece of documentation.

LV For whatever reason, that file was deleted from www.tcl.tk . Perhaps all the deprecated commands were removed? I'm uncertain.


Suppose you have some old code using this command, and as a good Tcl citizen you want to follow the advice above. But how are you supposed to rewrite the line and use switch instead if there is no specification to be found of the old command?

Note that it is not as simple as substituting "case" by "'switch"...


RS 2005-12-04 - Remember that Tcl has quite helpful error messages. I tried this:

 % case
 wrong # args: should be "case string ?in? patList body ... ?default body?"
 % case foo in {a* b*} {puts 1} {c* d*} {puts 2} {e* f*} {puts 3} 
 3

So it appears as if elements in patList are matched in glob style :) Compare this to the case control structure in Unix shells, e.g. in bash:

    case word in [ [(] pattern [ | pattern ] ... ) list  ;;  ]
    ... esac
           A case command first expands  word,  and  tries  to
           match  it  against  each pattern in turn, using the
           same matching rules as for pathname expansion  (see
           Pathname  Expansion below).  When a match is found,
           the corresponding  list  is  executed.   After  the
           first  match,  no subsequent matches are attempted.
           The exit status is  zero  if  no  pattern  matches.
           Otherwise,  it  is the exit status of the last com-
           mand executed in list.

with the bad voodoo of using ";;" as case separator, and esac as end marker...


Bernard Desgraupes on the Alpha developers list kindly provided the man page, digged out from the 8.0.5 distribution:

 NAME

 case - Evaluate one of several scripts, depending on a given value


 SYNOPSIS

 case string ?in? patList body ?patList body ...?
 case string ?in? {patList body ?patList body ...?}


 DESCRIPTION

 Note: the case command is obsolete and is supported only for backward 
 compatibility. At some point in the future it may be removed entirely. 
 You should use the switch command instead.

 The case command matches string against each of the patList arguments in 
 order. Each patList argument is a list of one or more patterns. If any 
 of these patterns matches string then case evaluates the following body 
 argument by passing it recursively to the Tcl interpreter and returns 
 the result of that evaluation. Each patList argument consists of a 
 single pattern or list of patterns. Each pattern may contain any of the 
 wild-cards described under string match. If a patList argument is 
 default, the corresponding body will be evaluated if no patList matches 
 string. If no patList argument matches string and no default is given, 
 then the case command returns an empty string.

 Two syntaxes are provided for the patList 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 case 
 commands, since the braces around the whole list make it unnecessary to 
 include a backslash at the end of each line. Since the patList 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.


 KEYWORDS

 case, match, regular expression