1prefix(n) Tcl Built-In Commands prefix(n)
2
3
4
5______________________________________________________________________________
6
8 tcl::prefix - facilities for prefix matching
9
11 ::tcl::prefix all table string
12 ::tcl::prefix longest table string
13 ::tcl::prefix match ?option ...? table string
14______________________________________________________________________________
15
17 This document describes commands looking up a prefix in a list of
18 strings. The following commands are supported:
19
20 ::tcl::prefix all table string
21 Returns a list of all elements in table that begin with the pre‐
22 fix string.
23
24 ::tcl::prefix longest table string
25 Returns the longest common prefix of all elements in table that
26 begin with the prefix string.
27
28 ::tcl::prefix match ?options? table string
29 If string equals one element in table or is a prefix to exactly
30 one element, the matched element is returned. If not, the result
31 depends on the -error option. (It is recommended that the table
32 be sorted before use with this subcommand, so that the list of
33 matches presented in the error message also becomes sorted,
34 though this is not strictly necessary for the operation of this
35 subcommand itself.)
36
37 -exact
38 Accept only exact matches.
39
40 -message string
41 Use string in the error message at a mismatch. Default is
42 “option”.
43
44 -error options
45 The options are used when no match is found. If options
46 is empty, no error is generated and an empty string is
47 returned. Otherwise the options are used as return
48 options when generating the error message. The default
49 corresponds to setting “-level 0”. Example: If “-error
50 {-errorcode MyError -level 1}” is used, an error would be
51 generated as:
52
53 return -errorcode MyError -level 1 -code error \
54 "ambiguous option ..."
55
57 Basic use:
58
59 namespace import ::tcl::prefix
60 prefix match {apa bepa cepa} apa
61 → apa
62 prefix match {apa bepa cepa} a
63 → apa
64 prefix match -exact {apa bepa cepa} a
65 → bad option "a": must be apa, bepa, or cepa
66 prefix match -message "switch" {apa ada bepa cepa} a
67 → ambiguous switch "a": must be apa, ada, bepa, or cepa
68 prefix longest {fblocked fconfigure fcopy file fileevent flush} fc
69 → fco
70 prefix all {fblocked fconfigure fcopy file fileevent flush} fc
71 → fconfigure fcopy
72
73 Simplifying option matching:
74
75 array set opts {-apa 1 -bepa "" -cepa 0}
76 foreach {arg val} $args {
77 set opts([prefix match {-apa -bepa -cepa} $arg]) $val
78 }
79
80 Creating a switch that supports prefixes:
81
82 switch [prefix match {apa bepa cepa} $arg] {
83 apa { }
84 bepa { }
85 cepa { }
86 }
87
89 lsearch(n), namespace(n), string(n), Tcl_GetIndexFromObj(3)
90
92 prefix, table lookup
93
94
95
96Tcl 8.6 prefix(n)