Joining list elements
While the concat
command is more than capable of merging lists and arguments into a single list, Tcl also provides the join
command to expand the functionality. The join
command will not only merge the arguments, but also allows insertion of a separation character into the returned string. Note that the variable provided must contain a list, set of lists, or standalone values. The syntax is as follows:
join list delimiter
How to do it…
In the following example, we will join the elements of a list and create a comma-delimited string. Return values from the commands are provided for clarity. Enter the following command:
% set input {John Mary Bill} John Mary Bill % join $input ", " John, Mary, Bill
How it works…
The join
command returns the complete string created by joining all elements of the variable provided. If a delimiter is specified, it will create a delimited list, otherwise the string will be returned space delimited.
There's more…
In the following example, we...