1<HTML><HEAD><TITLE>Built-In Commands - split manual page</TITLE></HEAD><BODY> 2<H3><A NAME="M2">NAME</A></H3> 3split - Split a string into a proper Tcl list 4<H3><A NAME="M3">SYNOPSIS</A></H3> 5<B>split </B><I>string </I>?<I>splitChars</I>?<BR> 6<H3><A NAME="M4">DESCRIPTION</A></H3> 7Returns a list created by splitting <I>string</I> at each character 8that is in the <I>splitChars</I> argument. 9Each element of the result list will consist of the 10characters from <I>string</I> that lie between instances of the 11characters in <I>splitChars</I>. 12Empty list elements will be generated if <I>string</I> contains 13adjacent characters in <I>splitChars</I>, or if the first or last 14character of <I>string</I> is in <I>splitChars</I>. 15If <I>splitChars</I> is an empty string then each character of 16<I>string</I> becomes a separate element of the result list. 17<I>SplitChars</I> defaults to the standard white-space characters. 18<H3><A NAME="M5">EXAMPLES</A></H3> 19Divide up a USENET group name into its hierarchical components: 20<PRE><B>split</B> "comp.lang.tcl.announce" . 21 <I>=> comp lang tcl announce</I></PRE> 22<P> 23See how the <B>split</B> command splits on <I>every</I> character in 24<I>splitChars</I>, which can result in information loss if you are not 25careful: 26<PRE><B>split</B> "alpha beta gamma" "temp" 27 <I>=> al {ha b} {} {a ga} {} a</I></PRE> 28<P> 29Extract the list words from a string that is not a well-formed list: 30<PRE><B>split</B> "Example with {unbalanced brace character" 31 <I>=> Example with \{unbalanced brace character</I></PRE> 32<P> 33Split a string into its constituent characters 34<PRE><B>split</B> "Hello world" {} 35 <I>=> H e l l o { } w o r l d</I></PRE> 36<H3><A NAME="M6">PARSING RECORD-ORIENTED FILES</A></H3> 37Parse a Unix /etc/passwd file, which consists of one entry per line, 38with each line consisting of a colon-separated list of fields: 39<PRE>## Read the file 40set fid [open /etc/passwd] 41set content [read $fid] 42close $fid 43 44## Split into records on newlines 45set records [<B>split</B> $content "\n"] 46 47## Iterate over the records 48foreach rec $records { 49 50 ## Split into fields on colons 51 set fields [<B>split</B> $rec ":"] 52 53 ## Assign fields to variables and print some out... 54 lassign $fields \ 55 userName password uid grp longName homeDir shell 56 puts "$longName uses [file tail $shell] for a login shell" 57}</PRE> 58<H3><A NAME="M7">SEE ALSO</A></H3> 59<B><A HREF="../TclCmd/join.htm">join</A></B>, <B><A HREF="../TclCmd/list.htm">list</A></B>, <B><A HREF="../TclCmd/string.htm">string</A></B> 60<H3><A NAME="M8">KEYWORDS</A></H3> 61<A href="../Keywords/L.htm#list">list</A>, <A href="../Keywords/S.htm#split">split</A>, <A href="../Keywords/S.htm#string">string</A> 62<HR><PRE> 63<A HREF="../copyright.htm">Copyright</A> © 1993 The Regents of the University of California. 64<A HREF="../copyright.htm">Copyright</A> © 1994-1996 Sun Microsystems, Inc. 65<A HREF="../copyright.htm">Copyright</A> © 1995-1997 Roger E. Critchlow Jr.</PRE> 66</BODY></HTML> 67