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 ...
Get Tcl/Tk 8.5 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.