[25] | 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 | '\" RCS: @(#) $Id: split.n,v 1.10 2007/12/13 15:22:33 dgp Exp $ |
---|
| 9 | '\" |
---|
| 10 | .so man.macros |
---|
| 11 | .TH split n "" Tcl "Tcl Built-In Commands" |
---|
| 12 | .BS |
---|
| 13 | '\" Note: do not modify the .SH NAME line immediately below! |
---|
| 14 | .SH NAME |
---|
| 15 | split \- Split a string into a proper Tcl list |
---|
| 16 | .SH SYNOPSIS |
---|
| 17 | \fBsplit \fIstring \fR?\fIsplitChars\fR? |
---|
| 18 | .BE |
---|
| 19 | |
---|
| 20 | .SH DESCRIPTION |
---|
| 21 | .PP |
---|
| 22 | Returns a list created by splitting \fIstring\fR at each character |
---|
| 23 | that is in the \fIsplitChars\fR argument. |
---|
| 24 | Each element of the result list will consist of the |
---|
| 25 | characters from \fIstring\fR that lie between instances of the |
---|
| 26 | characters in \fIsplitChars\fR. |
---|
| 27 | Empty list elements will be generated if \fIstring\fR contains |
---|
| 28 | adjacent characters in \fIsplitChars\fR, or if the first or last |
---|
| 29 | character of \fIstring\fR is in \fIsplitChars\fR. |
---|
| 30 | If \fIsplitChars\fR is an empty string then each character of |
---|
| 31 | \fIstring\fR becomes a separate element of the result list. |
---|
| 32 | \fISplitChars\fR defaults to the standard white-space characters. |
---|
| 33 | .SH EXAMPLES |
---|
| 34 | Divide up a USENET group name into its hierarchical components: |
---|
| 35 | .CS |
---|
| 36 | \fBsplit\fR "comp.lang.tcl.announce" . |
---|
| 37 | \fI\(-> comp lang tcl announce\fR |
---|
| 38 | .CE |
---|
| 39 | .PP |
---|
| 40 | See how the \fBsplit\fR command splits on \fIevery\fR character in |
---|
| 41 | \fIsplitChars\fR, which can result in information loss if you are not |
---|
| 42 | careful: |
---|
| 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 | .CS |
---|
| 50 | \fBsplit\fR "Example with {unbalanced brace character" |
---|
| 51 | \fI\(-> Example with \e{unbalanced brace character\fR |
---|
| 52 | .CE |
---|
| 53 | .PP |
---|
| 54 | Split a string into its constituent characters |
---|
| 55 | .CS |
---|
| 56 | \fBsplit\fR "Hello world" {} |
---|
| 57 | \fI\(-> H e l l o { } w o r l d\fR |
---|
| 58 | .CE |
---|
| 59 | .SS "PARSING RECORD-ORIENTED FILES" |
---|
| 60 | Parse a Unix /etc/passwd file, which consists of one entry per line, |
---|
| 61 | with each line consisting of a colon-separated list of fields: |
---|
| 62 | .CS |
---|
| 63 | ## Read the file |
---|
| 64 | set fid [open /etc/passwd] |
---|
| 65 | set content [read $fid] |
---|
| 66 | close $fid |
---|
| 67 | |
---|
| 68 | ## Split into records on newlines |
---|
| 69 | set records [\fBsplit\fR $content "\en"] |
---|
| 70 | |
---|
| 71 | ## Iterate over the records |
---|
| 72 | foreach rec $records { |
---|
| 73 | |
---|
| 74 | ## Split into fields on colons |
---|
| 75 | set fields [\fBsplit\fR $rec ":"] |
---|
| 76 | |
---|
| 77 | ## Assign fields to variables and print some out... |
---|
| 78 | lassign $fields \e |
---|
| 79 | userName password uid grp longName homeDir shell |
---|
| 80 | puts "$longName uses [file tail $shell] for a login shell" |
---|
| 81 | } |
---|
| 82 | .CE |
---|
| 83 | |
---|
| 84 | .SH "SEE ALSO" |
---|
| 85 | join(n), list(n), string(n) |
---|
| 86 | |
---|
| 87 | .SH KEYWORDS |
---|
| 88 | list, split, string |
---|