| 1 |
'\" |
| 2 |
'\" Copyright (c) 1993 The Regents of the University of California. |
| 3 |
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH split n "" Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
split \- Split a string into a proper Tcl list |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBsplit \fIstring \fR?\fIsplitChars\fR? |
| 16 |
.BE |
| 17 |
.SH DESCRIPTION |
| 18 |
.PP |
| 19 |
Returns a list created by splitting \fIstring\fR at each character |
| 20 |
that is in the \fIsplitChars\fR argument. |
| 21 |
Each element of the result list will consist of the |
| 22 |
characters from \fIstring\fR that lie between instances of the |
| 23 |
characters in \fIsplitChars\fR. |
| 24 |
Empty list elements will be generated if \fIstring\fR contains |
| 25 |
adjacent characters in \fIsplitChars\fR, or if the first or last |
| 26 |
character of \fIstring\fR is in \fIsplitChars\fR. |
| 27 |
If \fIsplitChars\fR is an empty string then each character of |
| 28 |
\fIstring\fR becomes a separate element of the result list. |
| 29 |
\fISplitChars\fR defaults to the standard white-space characters. |
| 30 |
.SH EXAMPLES |
| 31 |
.PP |
| 32 |
Divide up a USENET group name into its hierarchical components: |
| 33 |
.PP |
| 34 |
.CS |
| 35 |
\fBsplit\fR "comp.lang.tcl" . |
| 36 |
\fI\(-> comp lang tcl\fR |
| 37 |
.CE |
| 38 |
.PP |
| 39 |
See how the \fBsplit\fR command splits on \fIevery\fR character in |
| 40 |
\fIsplitChars\fR, which can result in information loss if you are not |
| 41 |
careful: |
| 42 |
.PP |
| 43 |
.CS |
| 44 |
\fBsplit\fR "alpha beta gamma" "temp" |
| 45 |
\fI\(-> al {ha b} {} {a ga} {} a\fR |
| 46 |
.CE |
| 47 |
.PP |
| 48 |
Extract the list words from a string that is not a well-formed list: |
| 49 |
.PP |
| 50 |
.CS |
| 51 |
\fBsplit\fR "Example with {unbalanced brace character" |
| 52 |
\fI\(-> Example with \e{unbalanced brace character\fR |
| 53 |
.CE |
| 54 |
.PP |
| 55 |
Split a string into its constituent characters |
| 56 |
.PP |
| 57 |
.CS |
| 58 |
\fBsplit\fR "Hello world" {} |
| 59 |
\fI\(-> H e l l o { } w o r l d\fR |
| 60 |
.CE |
| 61 |
.SS "PARSING RECORD-ORIENTED FILES" |
| 62 |
.PP |
| 63 |
Parse a Unix /etc/passwd file, which consists of one entry per line, |
| 64 |
with each line consisting of a colon-separated list of fields: |
| 65 |
.PP |
| 66 |
.CS |
| 67 |
## Read the file |
| 68 |
set fid [open /etc/passwd] |
| 69 |
set content [read $fid] |
| 70 |
close $fid |
| 71 |
|
| 72 |
## Split into records on newlines |
| 73 |
set records [\fBsplit\fR $content "\en"] |
| 74 |
|
| 75 |
## Iterate over the records |
| 76 |
foreach rec $records { |
| 77 |
|
| 78 |
## Split into fields on colons |
| 79 |
set fields [\fBsplit\fR $rec ":"] |
| 80 |
|
| 81 |
## Assign fields to variables and print some out... |
| 82 |
lassign $fields \e |
| 83 |
userName password uid grp longName homeDir shell |
| 84 |
puts "$longName uses [file tail $shell] for a login shell" |
| 85 |
} |
| 86 |
.CE |
| 87 |
.SH "SEE ALSO" |
| 88 |
join(n), list(n), string(n) |
| 89 |
.SH KEYWORDS |
| 90 |
list, split, string |
| 91 |
'\" Local Variables: |
| 92 |
'\" mode: nroff |
| 93 |
'\" End: |