O'Reilly logo

Practical Programming in Tcl & Tk, Third Edition by Brent B. Welch

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Modifying Lists: linsert and lreplace

The linsert command inserts elements into a list value at a specified index. If the index is zero or less, then the elements are added to the front. If the index is equal to or greater than the length of the list, then the elements are appended to the end. Otherwise, the elements are inserted before the element that is currently at the specified index.

lreplace replaces a range of list elements with new elements. If you don't specify any new elements, you effectively delete elements from a list.

Note: linsert and lreplace do not modify an existing list. Instead, they return a new list value. In the following example, the lreplace command does not change the value of x:

Example 5-5 Modifying lists with linsert ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required