0

Ho do I specify multiple values to a key in Tcl associative array?

set A(a) val1
set A(a) val2

This is overriding the value and, on printing, I get the last value. How to store and to retrieve both of them?

1 Answer 1

2

You could store the values in a list, and store that list into the array:

set A(a) [list val1]
lappend A(a) val2
puts $A(a)
# val1 val2
Sign up to request clarification or add additional context in comments.

2 Comments

This is the standard way to do this, and that's true across many languages (after allowing for syntax and so on).
Note: There is no need to use a different approach for the first and subsequent elements. You can use lappend for all. The lappend command will behave the same as the set/list combination shown in the code above if the array element does not exist.

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.