Determining the number of elements
To provide more flexibility to our programs, it will be necessary to create and maintain your lists dynamically. As this will result in a list of indeterminate size, we need a means to determine the number of elements in a list. To accomplish this, Tcl provides the llength
command. The syntax is as follows:
llength list
How to do it…
In the following example, we will pass a list to the llength
command to determine the number of elements the list contains. Return values from the commands are provided for clarity. Enter the following command:
% llength {John Mary { Bill Tom }} 3
How it works…
The llength
command accepts a list (in this case, a list containing an embedded list with multiple elements) as an argument and returns a decimal string containing the number of elements. As the embedded list is a single item, in the parent list the total is 3
.