switch(3tcl)

✨ NAME ✨

so like, switch(3tcl) bestie β€” it evaluates one of several scripts, depending on a given value. that's the whole vibe. πŸ’–


πŸŽ€ SYNOPSIS πŸŽ€

switch ?options? string pattern body ?pattern body ...?

switch ?options? string {pattern body ?pattern body ...?}

two whole forms, pick your fighter 🫢


🌸 DESCRIPTION 🌸

okay so here's the tea. the switch command takes its string argument and matches it against each of the pattern arguments, in order. the absolute second it finds a pattern that matches string, it evaluates the body argument that comes right after it β€” by passing it recursively to the Tcl interpreter β€” and returns the result of that evaluation. that's it. that's the whole thing. πŸ’…

if the last pattern argument is default then it matches anything. iconic behavior, honestly. and if no pattern argument matches string and there's no default given? then switch just returns an empty string. it's giving "no thoughts, head empty." 😭

now β€” if the initial arguments to switch start with - then they get treated as options, unless there are exactly two arguments to switch (in which case the first one has to be the string and the second one has to be the pattern/body list). no cap.

πŸ‘‘ the options πŸ‘‘

the following options are currently supported:

  • -exact β€” use exact matching when comparing string to a pattern. this is the default, bestie, it's the baseline slay.
  • -glob β€” when matching string to the patterns, use glob-style matching (i.e. the same as implemented by the string match command). wildcards, but make it fashion. πŸ¦‹
  • -regexp β€” when matching string to the patterns, use regular expression matching (as described in the re_syntax reference page). this is the main character option, ngl. πŸ”₯
  • -nocase β€” causes comparisons to be handled in a case-insensitive manner. uppercase who? never heard of her. πŸ’
  • -matchvar varName β€” this option (only legal when -regexp is also specified) specifies the name of a variable into which the list of matches found by the regular expression engine will be written. the first element of the list written will be the overall substring of the input string (i.e. the string argument to switch) matched, the second element of the list will be the substring matched by the first capturing parenthesis in the regular expression that matched, and so on. when a default branch is taken, the variable will have the empty list written to it. this option may be specified at the same time as the -indexvar option β€” they're besties, they can hang out together. 🫢
  • -indexvar varName β€” this option (only legal when -regexp is also specified) specifies the name of a variable into which the list of indices referring to matching substrings found by the regular expression engine will be written. the first element of the list written will be a two-element list specifying the index of the start and index of the first character after the end of the overall substring of the input string (i.e. the string argument to switch) matched, in a similar way to the -indices option to the regexp can obtain. similarly, the second element of the list refers to the first capturing parenthesis in the regular expression that matched, and so on. when a default branch is taken, the variable will have the empty list written to it. this option may be specified at the same time as the -matchvar option. ✨
  • -- β€” marks the end of options. the argument following this one will be treated as string even if it starts with a -. this is not required when the matching patterns and bodies are grouped together in a single argument. it's the "ok we're done with flags now" moment. πŸ›‘

🧚 two syntaxes, two vibes 🧚

so there's two syntaxes for the pattern and body arguments:

  1. 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.
  2. 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 super easy to construct multi-line switch commands, since the braces around the whole list make it unnecessary to include a backslash at the end of each line. but β€” and this is the important part, don't skip it β€” since the pattern 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. lowkey a whole personality difference. πŸ™ˆ

πŸ’€ the - fall-through πŸ’€

if a body is specified as - it means that the body for the next pattern should also be used as the body for this pattern (if the next pattern also has a body of - then the body after that is used, and so on). this feature makes it possible to share a single body among several patterns. it's giving "we're all eating from the same plate." πŸ“

⚠️ beware of how you place comments in switch commands. comments should only be placed inside the execution body of one of the patterns, and not intermingled with the patterns. put a comment in the wrong spot and the vibes will be immaculate but the code will not. πŸ’€

🌈 EXAMPLES 🌈

the switch command can match against variables and not just literals, as shown here (the result is 2):

set foo "abc"
switch abc a - b {expr {1}} $foo {expr {2}} default {expr {3}}

using glob matching and the fall-through body is an alternative to writing regular expressions with alternations, as can be seen here (this returns 1):

switch -glob aaab {
    a*b     -
    b       {expr {1}}
    a*      {expr {2}}
    default {expr {3}}
}

whenever nothing matches, the default clause (which must be last) is taken. this example has a result of 3:

switch xyz {
    a -
    b {
        # Correct Comment Placement
        expr {1}
    }
    c {
        expr {2}
    }
    default {
        expr {3}
    }
}

when matching against regular expressions, information about what exactly matched is easily obtained using the -matchvar option:

switch -regexp -matchvar foo -- $bar {
    a(b*)c {
        puts "Found [string length [lindex $foo 1]] 'b's"
    }
    d(e*)f(g*)h {
        puts "Found [string length [lindex $foo 1]] 'e's and\
                [string length [lindex $foo 2]] 'g's"
    }
}

πŸ¦‹ SEE ALSO πŸ¦‹


⭐ KEYWORDS ⭐

  • switch
  • match
  • regular expression

and that's the whole page, bestie πŸ’– this was Tcl 8.5, switch(3tcl), mwah β€” go forth and match responsibly. πŸŽ€βœ¨

πŸ“¦ where this page comes from
  • ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms