prefix(3tcl)
π tcl::prefix β the prefix-matching bestie π
okay so this is a page from the Tcl Built-In Commands manual, section 3tcl, and it's literally all about looking up prefixes in lists of strings. No cap, it's giving "autocomplete but make it a Tcl command." π¦
β¨ NAME β¨
tcl::prefix β facilities for prefix matching πΈ
β¨ SYNOPSIS β¨
::tcl::prefix all table string
::tcl::prefix longest table string
::tcl::prefix match ?option ...? table string
β¨ DESCRIPTION β¨
So bestie, this whole doc is about commands that go looking for a prefix inside a list of strings. Here's the lineup, and yes they all ate: π
::tcl::prefix all table stringβ hands you back a list of every element in table that starts with the prefix string. All of them. The whole squad. π::tcl::prefix longest table stringβ gives you the longest common prefix of all the elements in table that begin with the prefix string. Like, how far can they all agree before they start diverging? π::tcl::prefix match ?option ...? table stringβ the main character, fr fr. If string is equal to one element in table, or it's a prefix of exactly one element, then boom, you get that matched element back. But if it doesn't match? Then it all depends on the-erroroption, no pressure. (Also, tiny rec: it's recommended that you sort table before using it with this subcommand, so that the list of matches in the error message comes out sorted too. It's not strictly necessary for this subcommand to actually work, though β it's just a vibe thing. π)
The options it supports are:
-exactβ only exact matches count. No partial credit, periodt. β-messagestring β use string in the error message when there's a mismatch. The default is βoptionβ. π¬-erroroptions β these options get used when no match is found. If options is empty, then no error is generated at all and you just get an empty string back. Otherwise, the options are used as return options when generating the error message. The default corresponds to setting β-level 0β. For example: if β-error {-errorcode MyError -level 1}β is used, an error would be generated as:
return -errorcode MyError -level 1 -code error \
"ambiguous option ..."
β¨ EXAMPLES β¨
Basic use, babes:
namespace import ::tcl::prefix
prefix match {apa bepa cepa} apa
β apa
prefix match {apa bepa cepa} a
β apa
prefix match -exact {apa bepa cepa} a
β bad option "a": must be apa, bepa, or cepa
prefix match -message "switch" {apa ada bepa cepa} a
β ambiguous switch "a": must be apa, ada, bepa, or cepa
prefix longest {fblocked fconfigure fcopy file fileevent flush} fc
β fco
prefix all {fblocked fconfigure fcopy file fileevent flush} fc
β fconfigure fcopy
Simplifying option matching (so clean, so satisfying π§):
array set opts {-apa 1 -bepa "" -cepa 0}
foreach {arg val} $args {
set opts([prefix match {-apa -bepa -cepa} $arg]) $val
}
Creating a switch that supports prefixes β iconic behavior honestly:
switch [prefix match {apa bepa cepa} $arg] {
apa { }
bepa { }
cepa { }
}
β¨ SEE ALSO β¨
lsearch(3tcl), namespace(3tcl), string(3tcl), Tcl_GetIndexFromObj(3tcl) π«Ά
β¨ KEYWORDS β¨
prefix, table lookup β
okay that's the whole page, you're officially a prefix-matching girlie now π This one's from Tcl 8.6, it's the prefix(3tcl) page, and it lives in the Tcl Built-In Commands manual. Mwah, go forth and match those prefixes. π
ππ¦
π¦ where this page comes from
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2008 Peter Spjuth <pspjuth@users.sourceforge.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms