1split(n) Tcl Built-In Commands split(n)
2
3
4
5______________________________________________________________________________
6
8 split - Split a string into a proper Tcl list
9
11 split string ?splitChars?
12_________________________________________________________________
13
14
16 Returns a list created by splitting string at each character that is in
17 the splitChars argument. Each element of the result list will consist
18 of the characters from string that lie between instances of the charac‐
19 ters in splitChars. Empty list elements will be generated if string
20 contains adjacent characters in splitChars, or if the first or last
21 character of string is in splitChars. If splitChars is an empty string
22 then each character of string becomes a separate element of the result
23 list. SplitChars defaults to the standard white-space characters.
24
26 Divide up a USENET group name into its hierarchical components:
27 split "comp.lang.tcl.announce" .
28 → comp lang tcl announce
29
30 See how the split command splits on every character in splitChars,
31 which can result in information loss if you are not careful:
32 split "alpha beta gamma" "temp"
33 → al {ha b} {} {a ga} {} a
34
35 Extract the list words from a string that is not a well-formed list:
36 split "Example with {unbalanced brace character"
37 → Example with \{unbalanced brace character
38
39 Split a string into its constituent characters
40 split "Hello world" {}
41 → H e l l o { } w o r l d
42
43 PARSING RECORD-ORIENTED FILES
44 Parse a Unix /etc/passwd file, which consists of one entry per line,
45 with each line consisting of a colon-separated list of fields:
46 ## Read the file
47 set fid [open /etc/passwd]
48 set content [read $fid]
49 close $fid
50
51 ## Split into records on newlines
52 set records [split $content "\n"]
53
54 ## Iterate over the records
55 foreach rec $records {
56
57 ## Split into fields on colons
58 set fields [split $rec ":"]
59
60 ## Assign fields to variables and print some out...
61 lassign $fields \
62 userName password uid grp longName homeDir shell
63 puts "$longName uses [file tail $shell] for a login shell"
64 }
65
66
68 join(n), list(n), string(n)
69
70
72 list, split, string
73
74
75
76Tcl split(n)