Say I have an array in a TCL, and I want to change the value of the element which has a key say "First_elem". How I can do this?
1 Answer
Just set the array element like any other variable: set myArray(key) "value"
Here's a more complete example:
array set myArray {
key1 1234
key2 5678
}
echo $myArray(key1)
set myArray(key1) "test" // Change an existing element
set myArray(key3) "hello" // Add a new element
echo $myArray(key1)
4 Comments
Narek
Ok, but if it is a list (the value), can I change one elem in that list without touching the other elements of the list?
überjesus
You can do that with the
lreplace (replace an element in a list) function. This would replace the second list element of the key1-value in my example (the value has to be a list of course, which it is not in my example): set myArray(key1) [lreplace $myArray(key1) 1 1 "hello"]GrAnd
Instead of '
lreplace you can use lset. I.e. lset myArray(key1) 1 "new value".glenn jackman
echo? Take your shell hat off for a minute ;) Or interp alias "" echo "" puts