case(3tcl)

πŸ’– case(3tcl) β€” Tcl Built-In Commands πŸ’–

okay bestie, gather round, because we're looking at a Tcl Built-In Commands page and this one is... a little vintage πŸ‘΅βœ¨. It's case, it's from the Tcl era of 7.0, and it's giving "my grandma's flip phone that still works perfectly". Let's get into it 🫢


✨ NAME ✨

case β€” pick one script to run based on whatever value you handed it πŸŽ€


🌸 SYNOPSIS 🌸

case string ?in? patList body ?patList body ...?

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

πŸ’… DESCRIPTION πŸ’…

🚨 Not me being the bearer of bad news 🚨 but case is obsolete, bestie. It's only hanging around for backward compatibility. At some point in the future it may be removed entirely. So like... use the switch command instead, fr fr. 😭

okay so here's how the actual matching works πŸ‘‘

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. ✨

Each patList argument consists of a single pattern or a list of patterns. Each pattern may contain any of the wild-cards described under string match β€” yes, the whole wildcard buffet, help yourself πŸ“.

If a patList argument is default, then the corresponding body gets evaluated when no patList matches string. And if no patList argument matches string and no default is given? Then case just returns an empty string. Nothing. Silence. The void. πŸ’€


πŸ¦‹ The Two Syntaxes πŸ¦‹

Two syntaxes are provided for the patList and body arguments, and honestly they each have their own main character energy:

  1. The separate-arguments form β€” 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. Lowkey the flexible one πŸ’β€β™€οΈ
  2. The one-big-list form β€” places all of the patterns and commands together into a single argument. That 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. (No more \ at every line break, we love a convenience era 😌)
⚠️ but wait β€” 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 pick wisely, bestie, they are NOT the same girl πŸ’…

πŸ”₯ SEE ALSO πŸ”₯

  • switch(3tcl) β€” the upgrade, the glow-up, the one you should actually be using πŸ‘‘

πŸ‘‘ KEYWORDS πŸ‘‘

case, match, regular expression


and that's the whole page, no notes πŸ’– this was Tcl, version 7.0, filed under case(3tcl) β€” shoutout to the Tcl Built-In Commands manual for keeping this relic alive. slay, stay obsolete, bye bestie 🌈✨🫢

πŸ“¦ where this page comes from