Version 9 of join

Updated 2004-04-01 06:48:47

http://www.purl.org/tcl/home/man/tcl8.4/TclCmd/join.htm

Join converts a Tcl list into a string. It glues together the elements of a list using a supplied string (was: character) as element separator. Default is a space.

######## In the sentence above: character or string? RS: Of course it must read string - characters in Tcl are just strings of length 1, and it can easily tested that join accepts longer or shorter strings too:

 % join {a b c} " and "
 a and b and c
 % join {a b c} ""
 abc

Even after many years Tcling, old concepts spook around sometimes... :)

RS A typical application is to process a text that consists of several lines:

 foreach line [split $input \n] {
    # do something with line, produce output
    lappend outlines $out
 }
 set result [join $outlines \n] 

Another is to flatten out one level of embedded lists:

 % join {1 {2 {3 4}} 5}
 1 2 {3 4} 5

Matrixes are a frequent application for nested lists. Here's a simple matrix summer that uses join twice: once for concatenating the rows; then for putting + signs in between so expr has something to parse:

 proc matsum matrix {expr [join [join $matrix] +]}

Tcl syntax help - Arts and crafts of Tcl-Tk programming - Category Command