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 will join two lists in the same manner as the concat command and convert the lists into a single string value. Return values from the commands are provided for clarity. Enter the following command:


% set input {{John Mary Bill} {Tom Fred Sally}}
{John Mary Bill} {Tom Fred Sally}

% join $input
John Mary Bill Tom Fred Sally

As you can see, the join command has accepted the arguments and returned a string containing the elements of the two lists flattened.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.189.186