made_in_india
made_in_india

Reputation: 2279

Tcl access array key from a value declare in another file

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?

Upvotes: 1

Views: 184

Answers (2)

Sharad
Sharad

Reputation: 10612

You may also use:

puts [set ::foo($key)]

Upvotes: 1

Dinesh
Dinesh

Reputation: 16438

You can use

puts $::foo($key)

Within braces, substitution won't happen.

Upvotes: 3

Related Questions