Joining two lists

Now that we have seen how to make a list we will explore combining lists. To accomplish this, Tcl provides the concat command. The syntax is as follows:

	concat value1 value2 …

How to do it…

In the following example, we will concatenate a set of lists containing single characters. Return values from the commands are provided for clarity. Enter the following command:


% concat {a b c} {1 2 3}
a b c 1 2 3

How it works…

The concat command joins each of its arguments together with spaces after first trimming all leading and trailing whitespace, and in the case of a list, the results will be flattened. Although this command will concatenate any arguments provided, we will be focusing on its usage as it applies to the list elements. To denote that a list is being provided as the argument, it should be encased within braces {}. This is another method of providing lists. They may also be passed as named list variables, actual list commands, or within quotes. If no arguments are provided, it will return an empty string.

There's more…

In the following example, we will concatenate a list containing string values with additional string values to illustrate using concat to expand the elements in our list. Return values from the commands are provided for clarity. Enter the following command:


% concat {John Mary Bill} Tom Fred Sally
John Mary Bill Tom Fred Sally

As you can see in this example, the return value is a string containing not only our list but also the individual arguments provided.

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

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