split(n)

πŸ“¦
version 9.0.3 Β· 2 versions
  • 9.0.3 Β· alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0 (latest)
  • 8.6.10 Β· centos-stream 9
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 ⭐

  • list
  • split
  • string

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