Basically, every variable in Tcl can be considered as a string. The internal representation of such a variable depends on its execution state (see Chapter 2, Advanced Tcl features for more details). Being a dynamic language, Tcl converts the value of a variable to a suitable type at runtime. Let's consider the following example:
Note that in the last two cases, this pattern is put into curly braces to prevent the interpreter from considering the content of the []
braces as a command to be executed.
It is worth mentioning at this point that depending on the context, the variable's value may be treated as a text string, a number, or a logical value, just as we will see in the following examples.
More than pure strings, Tcl introduces the concept of two interesting data types—lists and arrays (hashmaps).
A Tcl list holds an ordered sequence of elements such as strings, other lists, and other variables. To create a list, you can use the list
command:
In this example, we create a list using three elements, separated with whitespaces, and assign the list to the myList
variable.
What is interesting is that there is no straight demarcation between the list and the string, and therefore, the following code also creates a list:
Curly braces are also possible here. The conclusion is that every string may be considered as a list. The advantage of using the list
command is that it takes care of correct formatting, adding braces and backslashes when necessary. Consider either of the following two commands:
They will create a list in which the third element will be another list consisting of elements specified in curly braces. The same effect can be achieved using the list
command:
From the user's point of view, a list is both a string and a list, because there is no visible difference:
As you can see, the list
command added the {}
braces at the correct points.
So why even bother with lists? Lists allow you to organize and structure your data, and the benefits of using them are that Tcl offers a set of commands that operates on lists. Lists tend to be extremely useful in typical scripts, therefore, we will briefly review the possible operations on them.
To get the number of elements in your list, use llength
. This command takes one parameter, that is, the list. The output is the number of elements in the list.
The command lindex
retrieves an element from a list. In basic usage, it takes the list and the index of the element as arguments. Just as in other programming languages, indexes start from zero in Tcl.
We can also specify the index as the special keyword end
, which indicates the last element. For example:
We can also specify end-<n>
, which indicates the nth
element from the end. The value end-0
is the same as the end
value; the value end-1
specifies the last but one item and so on. For example:
If the retrieved element is another list, lindex
also has the ability to retrieve its element by providing additional indexes:
In this example, the third element (its index value is 2)
of myList
is, in turn, another embedded list, and we retrieve its first element—string another
—by specifying an additional index value to lindex — 0
.
The lrange
command returns a subset of the original list. It accepts the list as the first argument followed by the index of the first and last items that should be included in the new list. Both the first and last elements are included in the resulting list. The index can be in the form of an integer or in the end
or end-<n>
forms.
This command returns a new list consisting of elements that is the start to end indexes:
In this case, 1
is the starting index and 2
the ending one, so the returned list consists of the second and third elements of the original list.
We can use the lappend
command to add one or more elements to an existing list. It accepts the variable name of the list, followed by any number of elements to be added to the list. The command inserts new elements at the end of the specified variable and does not return the new list—instead, it is automatically set as the new value of the specified variable.
For example:
This command can also be used to create new lists if the variable passed as the first argument does not exist.
The linsert
command can be used to insert one or more elements into a list at the specified index. Unlike lappend
, it takes the value of the list as its first argument, followed by the index to insert elements at and any number of elements to insert. For example:
As for any other list-related command, the index can be in the form of an integer or in the end
or end-<n>
forms.
The lset
command allows you to replace the values of elements with new ones. It accepts a variable name, and one or more indexes, followed by the new value to set. The changed list is stored back in the variable specified as the first argument.
For example, we can specify a single index to modify the nth
element of a list:
Similar to lindex
, this command can access and modify elements of sub-lists. For example:
The lreplace
command is also similar to it, but allows replacing a set of elements. It accepts the list as the first argument, the start and end indexes to replace, and the new items to insert instead of the specified elements. The command removes elements between start and end inclusively and inserts new items in their place. Like linsert
, it does not alter an existing list, but returns a new one. For example:
In case you don't specify new replacement items, the command will only remove elements within a range:
The lsort
command can be used to sort lists in Tcl. For example:
It is also possible to modify the sorting order by specifying additional options, for example -decreasing:
By default, the elements are compared as text values—therefore, when sorting 9, 10, 11, and, 2, the result would be 10, 11, 2, and 9. The following switches allow us to define how elements are compared, which affects the output of the sorting operation:
Dictionary-based sorting is similar to text-based comparison, except that character case is ignored here. An exception is when both the elements are the same, in that case, the uppercase elements are put before lowercase elements. If strings contain numbers, they are compared as integers, not characters. For example, when sorting 9, 10, 11, and 2 using the -dictionary
switch, the result would be 2, 9, 10, and 11.
We can also tell lsort
to use all elements in the list as sub-lists and compare them only using specified elements of these sub-lists, by using the -index
option. The value for the option can be a single index or a list of indexes, where each element of the index list indicates an index in a sub-list. For example:
Another option is to use your own custom implementation of the comparison algorithm. To do so, you have to define a command that accepts two arguments (items from the list that is being sorted) and returns the integer value by complying to the following rules:
The value is negative when the first argument is considered to be lower than the second
The value is 0 when both arguments are considered equal
The value is a positive value when the first argument is higher than the second one
It is illustrated in the following example:
We have the animals
list that contains some animal names. The default implementation will sort it in the alphabetical order, but we will be able to define our own implementation in the form of the compareString
procedure, which will sort it in the order of the word's length:
An interesting property of the lsort
command is that when two elements are considered as the same, their order in the output is the same as the order in which they were taken as inputs. This can be used to sort lists of lists using multiple orders. We need to sort the list by each element we want it to be sorted using, but in the reverse order.
For example, we can sort a list where each element is a sub-list consisting of first name, last name, and age. If we want to order it by the last name and the decreasing order of age for the same last names, we need to sort it by age first and later by the last name:
The result of this sort would be:
You can search through your list to find the element(s) that match your pattern using lsearch:
This command looks for the element three
in myList
, and returns the index of the first occurrence of the searched element—3 (or -1
if element is not found). It will be able to accept options such as:
-glob
—specifies that a pattern is in the glob-style (similar to file mask, see the string match
command's description for details: http://www.tcl.tk/man/tcl8.5/TclCmd/string.htm)
-regexp
—similar to the previous option, but the pattern is compliant with the regular expression's syntax (http://www.tcl.tk/man/tcl8.5/TclCmd/re_syntax.htm)
-inline
—the command will return the value of the element found instead of the index
-all
—the command will return a list of all matching occurrences, instead of just the first one
These options can be combined together; for your convenience, here is an example of their usage:
The expression *t*
will match any string containing the letter t
and the regular expression e$
will match any string that ends with the letter e
.
The output will be:
The command lassign
allows you to assign values from the list to different variables on a single line. For example:
The effect of this command is equal to the execution of the following:
The first argument is a list, and the consecutive arguments are names of the variables. If there are more variables than list elements, the remaining elements will be set to empty strings.
Another important data type for storing a set of values is an array. In Tcl, arrays are essentially associative maps, because any string value is permitted as an array key (index). Consider the following example:
After looking at the first line, the interpreter knows that the myArray
variable will be an array, because of presence of the ()
braces. Inside these braces, the array key key1
is written, and it is mapped to the string element1
. We have three keys overall—key1
, key2
, and 3
(note that the last one is different from the first two, but still valid)—that point to three values: element1, element2
, and element3
respectively. The key name can contain any character, and also spaces (of course, with the correct syntax, for example, by using the Esc key with a backslash).
To manipulate array variables, use the array
command with the appropriate operator and arguments. The most common operations are:
The command array exists
returns the value 1
only when the variable passed as an argument exists and is an array:
Otherwise, the command will return 0.
We can use the array unset
command to delete an array:
We can also delete a specified key or keys by specifying the pattern to match the keys that we want to delete as follows:
This will delete all the elements of an array that starts with prefix
,.
You can convert an array into a list using array get:
The list it returns consists of name-value pairs. This means that for each element in the array, this command returns its name as an element followed by its value as another element.
We can also use array get
to get specific elements of an array, by adding a pattern to match keys, similar to array unset
. For example:
This will return information only for keys starting with prefix
,.
To reverse this operation and set one or more keys in an array from the name-value pairs, we can use array set
command:
If you want to get a list of key names existing in a particular array, the array names
command comes handy:
Similarly, we can get only a subset of names by running:
This will return only the keys which start with prefix
,.
array size
returns the number of key-value mappings:
Although arrays are popular and commonly used, there are some drawbacks to using them. They are not Tcl objects on their own, but rather a collection of variables, a way in which a particular variable name can store zero or more key-value relations:
If we set an array key with the previous command, myarray
is not a variable by itself, but rather each key and value pair is stored as a Tcl object. From the programming perspective, this has little impact. However, once you set an array, you cannot pass it as a variable; for example:
The preceding code will raise an error (we will present return
later in this chapter). However, it is okay to reference it via the variable name in commands such as upvar, variable
, which are explained later throughout Chapter 2.
Also, only one dimensional arrays are supported—so the syntax like myArray(1)(2)
is incorrect. This can be bypassed by using some specific key name convention, for example with the '.' or ':' characters:
Effectively, my3DArray
can be treated as a three-dimensional array, with indexes separated by the character (or string) of your choice.
Flow control and loop instructions
Like almost any other language, Tcl has a set of commands—conditional and iterational—to control the flow of the program.
The first one is the command if:
Depending on the boolean value of the condition
variable, the first or the second part of the command is executed. In this case, the output is:
Note that the entire if
command call could be written in one line:
The first argument is an expression representing a logical condition. The value is evaluated in the same way as in case of the expr
command.
It is possible to skip else
and the second script body, or to add more conditions by using elseif
with another condition, and a script to execute in case the condition is true
.
The second command switch
allows us to decide what action should be taken based on the value passed; for example, consider the following example code:
As the value of value
variable is second
, the second script body is passed to the Tcl interpreter, resulting in the output second value on the screen.
It is possible to skip the default
section. Also, more than one acceptable value separated by the character may be defined for each of the sections:
The values 1, 1st
, or first
will all match with the first section.
Finally, it is possible to use patterns that we have used with string match
command to match the appropriate section, by specifying the -glob
option with the switch
command:
In this case, both the values second
and 2nd
would match with the second option.
Tcl offers three commands that allow the iterational execution of code: for, while
, and foreach
.
The following example presents the usage of the for
command:
The first argument is a code snippet executed at the beginning of the loop (in this example, it defines the counter variable x
and initializes it with value 1)
. The second one is a condition that is checked to be logically true before each loop (if the counter is smaller than 4) and the third is the condition which is evaluated after each loop—in most cases, to increase the counter (for example, using the command incr)
. The last part of the statement is the body of the loop. The output of the example is:
The while
command operates in a similar fashion, with the exception that the condition is evaluated after each loop, not before, and there is neither a start nor "after each loop" code section present. The equivalent of the previous example code written with while is:
The last command is foreach
. The idea behind this command is to pass one (or more) lists, and the loop's body is executed for every element of the list:
The following example shows using two lists at the same time. In every loop, consecutive elements from both lists are assigned to the variables amount
and item:
What is interesting here is that the lists are not required to be of the same length. If one of them is shorter, the empty value is used instead of using missing elements.
foreach
also allows us to fetch multiple variables in one shot—for example, to put all data in a three-column table in HTML, it's just as easy as:
Here $data
is a list.
It is possible to control the flow of loops with the commands break
and continue
. Using the first one will cause the loop to exit permanently, even when the end condition is not met. The second command causes the skipping of the current loop and the immediate start of the next one, or the end of the loop if the command that was skipped was the last one.