case(n)
π case(n) β the Tcl Built-In Commands glow-up π
okay so bestie, we're in the Tcl Built-In Commands section of the manual, and today's main character is case(n). buckle up π¦
β¨ 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, and this is important, bestie: thecasecommand is obsolete and is supported only for backward compatibility. at some point in the future it may be removed entirely. you should use theswitchcommand instead. no cap,switchis the vibe,caseis the deprecated era π
so here's the tea β β case 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. slay, that's the whole flow π«Ά
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. icon behavior honestly β
if a patList argument is default, the corresponding body will be evaluated if no patList matches string. and if no patList argument matches string and no default is given, then the case command returns an empty string. literally just vibes and nothing π
πΈ the two syntaxes (yes there are two, pick your fighter) πΈ
two syntaxes are provided for the patList and body arguments:
- the separate-arguments form β 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. very flexible, very demure π
- the one-big-list form β 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. this form makes it easy to construct multi-line
casecommands, since the braces around the whole list make it unnecessary to include a backslash at the end of each line. chef's kiss, no trailing-backslash suffering π₯
π but lowkey watch out: 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. so like, they're not interchangeable, bestie β pick the one that matches what you're actually doing. iykyk π
β¨ SEE ALSO β¨
switch(n)β the upgrade, the moment, the one you should actually be using π
β¨ KEYWORDS β¨
case, match, regular expression π
okay that's the whole page, you ate that π this little time capsule comes to you from Tcl, version 7.0, dated the 7.0 era, filed under the manual page name case(n) β go forth and maybe just use switch instead, mwah πβ¨
π¦ where this page comes from
- alpine 3.22: tcl-doc 8.6.16-r0 Β· TCL
- alpine 3.23: tcl-doc 8.6.17-r0 Β· TCL
- alpine 3.24: tcl-doc 8.6.17-r1 Β· TCL
- arch rolling: tcl 8.6.16-1 Β· TCL
- centos-stream 10: tcl-doc 1:8.6.13-4.el10 Β· TCL AND GPL-3.0-or-later WITH bison-exception-2.2 AND BSD-3-Clause
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms