Splitting a string into a list
The last thing we will be covering is how to take a string and split its content into a proper Tcl list. To accomplish this, Tcl provides the split
command. The syntax is as follows:
split string characters
How to do it…
In the following example, we will use the split command
to separate the contents of a comma-delimited string into a list. Return values from the commands are provided for clarity. Enter the following command:
% split {John,Mary,Tom,Fred,Sally}, John Mary Fred Tom Sally
How it works…
The split
command accepts a string and splits it into a Tcl list by splitting at every character defined. Caution must be exercised about the characters provided for splitting to avoid loss of data.