Replacing elements

To truly have a dynamic list we need the ability to replace existing elements with new values. To accomplish this, Tcl provides the lreplace command. The syntax is as follows:

	lreplace list first last element1 element2 …

How to do it…

In the following example, we will use the lreplace command replace elements contained within a list. Return values from the commands are provided for clarity. Enter the following command:


% lreplace {a b c d e} 1 1 X
a X c d e

How it works…

The lreplace command returns a newly created list formed by replacing one or more elements with the values contained within the element arguments. first and last refer to the indices specifying the first and last elements to be replaced. If the list is empty, ...

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.