Go to main content

man pages section 1: User Commands

Exit Print View

Updated: Wednesday, July 27, 2022
 
 

split (1t)

Name

split - Split a string into a proper Tcl list

Synopsis

split string ?splitChars?

Description

split(1t)                    Tcl Built-In Commands                   split(1t)



______________________________________________________________________________

NAME
       split - Split a string into a proper Tcl list

SYNOPSIS
       split string ?splitChars?
______________________________________________________________________________

DESCRIPTION
       Returns a list created by splitting string at each character that is in
       the splitChars argument.  Each element of the result list will  consist
       of the characters from string that lie between instances of the charac-
       ters in splitChars.  Empty list elements will be  generated  if  string
       contains  adjacent  characters  in  splitChars, or if the first or last
       character of string is in splitChars.  If splitChars is an empty string
       then  each character of string becomes a separate element of the result
       list.  SplitChars defaults to the standard white-space characters.

EXAMPLES
       Divide up a USENET group name into its hierarchical components:

              split "comp.lang.tcl" .
                    -> comp lang tcl

       See how the split command splits  on  every  character  in  splitChars,
       which can result in information loss if you are not careful:

              split "alpha beta gamma" "temp"
                    -> al {ha b} {} {a ga} {} a

       Extract the list words from a string that is not a well-formed list:

              split "Example with {unbalanced brace character"
                    -> Example with \{unbalanced brace character

       Split a string into its constituent characters

              split "Hello world" {}
                    -> H e l l o { } w o r l d

   PARSING RECORD-ORIENTED FILES
       Parse  a  Unix  /etc/passwd file, which consists of one entry per line,
       with each line consisting of a colon-separated list of fields:

              ## Read the file
              set fid [open /etc/passwd]
              set content [read $fid]
              close $fid

              ## Split into records on newlines
              set records [split $content "\n"]

              ## Iterate over the records
              foreach rec $records {

                  ## Split into fields on colons
                  set fields [split $rec ":"]

                  ## Assign fields to variables and print some out...
                  lassign $fields \
                          userName password uid grp longName homeDir shell
                  puts "$longName uses [file tail $shell] for a login shell"
              }


ATTRIBUTES
       See attributes(7) for descriptions of the following attributes:


       +---------------+------------------+
       |ATTRIBUTE TYPE | ATTRIBUTE VALUE  |
       +---------------+------------------+
       |Availability   | runtime/tcl-8    |
       +---------------+------------------+
       |Stability      | Uncommitted      |
       +---------------+------------------+

SEE ALSO
       join(n), list(n), string(n)

KEYWORDS
       list, split, string



NOTES
       Source code for open source software components in Oracle  Solaris  can
       be found at https://www.oracle.com/downloads/opensource/solaris-source-
       code-downloads.html.

       This    software    was    built    from    source     available     at
       https://github.com/oracle/solaris-userland.    The  original  community
       source was downloaded from  http://prdownloads.sourceforge.net/tcl/tcl-
       core8.6.7-src.tar.gz.

       Further information about this software can be found on the open source
       community website at https://www.tcl.tk/.



Tcl                                                                  split(1t)