1

I am working on main script that is souring a file eg foo.tcl In foo.tcl , we have some array variable declare eg

   array set foo {
       "john" "doe"
       "alpha" "beta"
   }

In the main script we are trying to access the value using the key of array

source foo.tcl
set key "john"
puts ${::foo($key)} ;# can't read "::foo($key)": no such element in array
puts ${::foo(john)} ;# not giving error

How to access pass the dynamic key to the array?

2 Answers 2

3

You can use

puts $::foo($key)

Within braces, substitution won't happen.

Sign up to request clarification or add additional context in comments.

Comments

1

You may also use:

puts [set ::foo($key)]

Comments

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.