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