Appending list elements
Up to this point, we have manually populated the elements of our list. While this is a usable method for list creation, it will become a necessity to programmatically populate the elements of a list at some point. To accomplish this, Tcl provides the lappend
command. The syntax is as follows:
lappend variable value1 value2 …
How to do it…
In the following example, we will append elements to a list. Return values from the commands are provided for clarity. Enter the following command:
% set input {John Mary Bill}
John Mary Bill
% lappend input Tom
John Mary Bill Tom
How it works…
The lappend
command has treated the variable name provided in variable as a list and appended all the following values to variable as list elements. ...
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.