SIMPLE SOLUTIONS

SPLIT(3TCL) - Linux man page online | Library functions

Split a string into a proper Tcl list.

Chapter
split(3tcl) Tcl Built-In Commands split(3tcl) _________________________________________________________________________________________________

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 characters in splitChars. Empty list elements will be gener‐ ated if string contains adjacent characters in splitChars, or if the first or last charac‐ ter 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 con‐ sisting 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" }

SEE ALSO

join(3tcl), list(3tcl), string(3tcl)

KEYWORDS

list, split, string
Tcl split(3tcl)
This manual Reference Other manuals
split(3tcl) referred by adjust(3tcl) | join(3tcl) | repeat(3tcl) | tabify(3tcl) | textutil(3tcl) | textutil_split(3tcl) | textutil_string(3tcl) | trim(3tcl)
refer to join(3tcl) | list(3tcl) | string(3tcl)
Download raw manual
Main page Tcl Built-In Commands (+154) Tcl (+812) № 3 (+68044)
Go top