2

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 1

5

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)
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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"]
Instead of 'lreplace you can use lset. I.e. lset myArray(key1) 1 "new value".
echo? Take your shell hat off for a minute ;) Or interp alias "" echo "" puts

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.