case(n)

πŸ“¦
version 8.6.17 Β· 2 versions
  • 8.6.17 Β· alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, opensuse-leap 16.0 (latest)
  • 8.6.10 Β· centos-stream 9
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, opensuse-leap 16.0

πŸŽ€ 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: 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. no cap, switch is the vibe, case is 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:

  1. 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 πŸ’…
  2. 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 case commands, 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