Editing a list

Another method of editing the contents of a Tcl list is the lset command. The syntax is as follows:

	lset variable index1 index 2 … value

How to do it…

In the following example, we will use the lset command to edit the contents of our list. Return values from the commands are provided for clarity. Enter the following command:


% set input {John Mary Fred}
John Mary Fred

% lset input 1 Tom
John Tom Fred

How it works…

The lset command accepts a variable containing a Tcl list and 0 or more indices. The indices may be entered singly or provided as a Tcl list. The value is the new value to place into the list in whole or in part if indices are provided.

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.