ZetCode

Tcl join Command

last modified October 20, 2026

The Tcl join command combines list elements into a string using a separator. It's useful for converting lists to strings with custom formatting.

Basic Definition

The join command takes a list and joins its elements with a specified separator string. If no separator is provided, a single space is used.

Syntax: join list ?joinString?. The first argument is the list to join. The optional second argument is the separator string.

Basic List Joining

This example shows the simplest usage of join to combine list elements.

basic_join.tcl
set colors {red green blue}
set result [join $colors]
puts $result

This joins the list elements without a separator argument, so the default separator, a single space, is used. The output will be "red green blue". To concatenate the elements directly, pass an empty string as the separator.

Joining With a Separator

Here we demonstrate joining list elements with a specified separator string.

join_separator.tcl
set fruits {apple banana orange}
set result [join $fruits ", "]
puts $result

This joins the fruit names with ", " as separator. The output will be "apple, banana, orange". The separator is inserted between each list element.

Joining With Different Separators

Different separators can be used to create various string formats from lists.

join_multiple_separators.tcl
set numbers {1 2 3 4 5}
puts [join $numbers " + "]
puts [join $numbers "->"]
puts [join $numbers "\n"]

This shows three different joining operations on the same list. The first creates a sum expression, the second makes an arrow sequence, and the third makes a vertical list.

Joining Nested Lists

The join command works with nested lists by joining at the top level only.

join_nested.tcl
set data {{a b} {c d} {e f}}
set result [join $data " | "]
puts $result

This joins the sublists with " | " as separator. The output will be "a b | c d | e f". Each sublist is first converted to a string (space-separated) before joining.

Creating CSV Data

The join command can help create CSV-formatted strings from lists.

join_csv.tcl
set row1 {Name Age Occupation}
set row2 {John 25 Engineer}
set row3 {Alice 30 Designer}

set csv [join [list [join $row1 ","] [join $row2 ","] [join $row3 ","]] "\n"]
puts $csv

This creates a CSV string with three rows. Each row is first joined with commas, then the rows are joined with newlines. The result is a simple CSV format; fields containing commas or quotes would need extra quoting.

Joining With Empty Separator

An empty separator string produces different results than omitting the separator, because the default separator is a space.

join_empty_separator.tcl
set letters {a b c d}
puts "No separator: [join $letters]"
puts "Empty separator: [join $letters ""]"

The first join (no separator) produces "a b c d" because the default separator is a space. The second join (empty separator) produces "abcd" because the elements are concatenated directly.

Best Practices

This tutorial covered the Tcl join command with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Tcl Tutorials.