Tcl lreplace Command
last modified April 3, 2025
The Tcl lreplace
command replaces elements in a list with new elements.
It returns a new list with the specified replacements while leaving the original
list unchanged. This is useful for list manipulation tasks.
Basic Definition
The lreplace
command replaces zero or more elements of a list with
new elements. It takes a list and index arguments specifying what to replace.
Syntax: lreplace list first last ?element element ...?
. The command
returns a new list with elements from first to last replaced by the new elements.
Basic List Replacement
This example demonstrates the simplest usage of lreplace
to replace
a single element in a list.
set colors {red green blue yellow} set new_colors [lreplace $colors 1 1 orange] puts $new_colors
This replaces the element at index 1 ("green") with "orange". The original list remains unchanged. The new list contains: red orange blue yellow.
Replacing Multiple Elements
lreplace
can replace a range of elements with multiple new elements.
set numbers {1 2 3 4 5 6} set new_numbers [lreplace $numbers 2 4 7 8 9] puts $new_numbers
This replaces elements from index 2 to 4 (3,4,5) with three new elements (7,8,9). The resulting list is: 1 2 7 8 9 6. The replacement count doesn't need to match.
Removing Elements
By not providing replacement elements, lreplace
can remove elements
from a list.
set fruits {apple banana cherry date elderberry} set trimmed [lreplace $fruits 1 3] puts $trimmed
This removes elements from index 1 to 3 (banana, cherry, date) without adding new elements. The result is a shorter list: apple elderberry.
Inserting Elements
When first and last are the same index, lreplace
inserts elements
without replacing any existing ones.
set letters {a b c d} set extended [lreplace $letters 2 2 x y z] puts $extended
This inserts elements x, y, z after index 2 without removing any elements. The result is: a b x y z c d. The original element at index 2 is shifted right.
Working with Nested Lists
lreplace
can handle nested lists, replacing entire sublists when needed.
set matrix {{1 2 3} {4 5 6} {7 8 9}} set new_matrix [lreplace $matrix 1 1 {10 11 12}] puts $new_matrix
This replaces the second sublist {4 5 6} with {10 11 12}. The result is a new matrix: {1 2 3} {10 11 12} {7 8 9}. The command works the same with nested lists.
End of List Operations
lreplace
can operate on the end of a list using the special index "end".
set items {pen pencil eraser ruler} set updated [lreplace $items end end marker] puts $updated
This replaces the last element "ruler" with "marker". The "end" index refers to the last element. The result is: pen pencil eraser marker.
Best Practices
- Immutability: Remember lreplace returns a new list without modifying the original.
- Indexing: Tcl uses 0-based indexing for list operations.
- Performance: For large lists, consider efficiency when replacing many elements.
- Error Handling: Check indices are valid to avoid errors.
- Combination: Combine with other list commands for complex operations.
This tutorial covered the Tcl lreplace
command with practical
examples showing its usage in different scenarios for list manipulation.
Author
List all Tcl Tutorials.