Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/split.n @ 37

Last change on this file since 37 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 2.6 KB
Line 
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
15split \- 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
22Returns a list created by splitting \fIstring\fR at each character
23that is in the \fIsplitChars\fR argument.
24Each element of the result list will consist of the
25characters from \fIstring\fR that lie between instances of the
26characters in \fIsplitChars\fR.
27Empty list elements will be generated if \fIstring\fR contains
28adjacent characters in \fIsplitChars\fR, or if the first or last
29character of \fIstring\fR is in \fIsplitChars\fR.
30If \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
34Divide 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
40See how the \fBsplit\fR command splits on \fIevery\fR character in
41\fIsplitChars\fR, which can result in information loss if you are not
42careful:
43.CS
44\fBsplit\fR "alpha beta gamma" "temp"
45      \fI\(-> al {ha b} {} {a ga} {} a\fR
46.CE
47.PP
48Extract 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
54Split 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"
60Parse a Unix /etc/passwd file, which consists of one entry per line,
61with each line consisting of a colon-separated list of fields:
62.CS
63## Read the file
64set fid [open /etc/passwd]
65set content [read $fid]
66close $fid
67
68## Split into records on newlines
69set records [\fBsplit\fR $content "\en"]
70
71## Iterate over the records
72foreach 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"
85join(n), list(n), string(n)
86
87.SH KEYWORDS
88list, split, string
Note: See TracBrowser for help on using the repository browser.