split(n)
π split(n) β the Tcl Built-In Commands era π
okay bestie, gather round, because we're talking about split, one of the Tcl built-in commands and honestly? she's a baddie. this is the one that takes a string and turns it into a proper Tcl list. no cap.
β¨ NAME β¨
split β split a string into a proper Tcl list π
πΈ SYNOPSIS πΈ
split string ?splitChars?
π DESCRIPTION π
so here's the tea. split returns a list, and that list is made by splitting string at every single character that shows up in the splitChars argument.
each element of the result list is gonna be the characters from string that were chilling between instances of the characters in splitChars. that's it, that's the whole vibe.
now, plot twist: empty list elements WILL be generated if:
- string has two characters from splitChars right next to each other (adjacent besties π), or
- the first or last character of string is in splitChars.
and if splitChars is an empty string, then literally every character of string becomes its own separate element of the result list. one character per element. main character energy for each letter, periodt.
oh and if you don't even pass splitChars? it defaults to the standard white-space characters. so it just knows. iykyk. π¦
π EXAMPLES π
dividing up a USENET group name into its hierarchical components β iconic:
split "comp.lang.tcl" .
β comp lang tcl
seeing how split splits on every character in splitChars β which, heads up, can result in information loss if you are not careful π:
split "alpha beta gamma" "temp"
β al {ha b} {} {a ga} {} a
extracting the list words from a string that is not a well-formed list β she's flexible like that:
split "Example with {unbalanced brace character"
β Example with \{unbalanced brace character
splitting a string into its constituent characters β baby letters, all separate:
split "Hello world" {}
β H e l l o { } w o r l d
π§ PARSING RECORD-ORIENTED FILES π§
this is the part where we parse a Unix /etc/passwd file. bestie it's one entry per line, and every line is a colon-separated list of fields. here's the whole thing, verbatim, go off:
## 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"
}
look at her go. splitting into records, splitting into fields, lassign-ing like a queen. π
π SEE ALSO π
(the whole friend group fr fr π«Ά)
β KEYWORDS β
listsplitstring
ok that's the whole page, no notes. now go split something. π
Tcl β split(n) β that's the source and the version right there at the bottom, bestie, and the manual name is folded in up top like we promised. slay, ate, no crumbs. πβ¨ππ¦πΈ
π¦ where this page comes from
- alpine 3.22: tcl-doc 8.6.16-r0 Β· TCL
- alpine 3.23: tcl-doc 8.6.17-r0 Β· TCL
- alpine 3.23: tcl9-doc 9.0.3-r0 Β· TCL
- alpine 3.24: tcl-doc 8.6.17-r1 Β· TCL
- alpine 3.24: tcl9-doc 9.0.3-r2 Β· TCL
- arch rolling: tcl 8.6.16-1 Β· TCL
- centos-stream 10: tcl-doc 1:8.6.13-4.el10 Β· TCL AND GPL-3.0-or-later WITH bison-exception-2.2 AND BSD-3-Clause
- fedora 43: tcl-doc 1:9.0.2-1.fc43 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- fedora 44: tcl-doc 1:9.0.2-1.fc44 Β· TCL AND GPL-3.0-or-later WITH Bison-exception-2.2 AND BSD-3-Clause
- nixos 26.05: tcl 9.0.3
- nixpkgs unstable: tcl 9.1.0
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- opensuse-leap 16.0: tcl 8.6.15-160000.2.2 Β· TCL
- Copyright (c) 1993 The Regents of the University of California
- Copyright (c) 1994-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms