I want to know how to make or create a global Array in TCL, to access it inside the Procedure
. I mean, If I have a Procedure
and I want to get the Array values to use it inside the Procedure, how can I do that?
0
votes
You might benefit from reading the Tcl tutorial -- variable scope is covered in chapter 16.
– glenn jackman
1 Answers
2
votes
You can use global
to access the array. E.g.
array set myArr {a 1 b 2 c 3}
proc foo {} {
global myArr
parray myArr
}
foo
#=> myArr(a) = 1
#=> myArr(b) = 2
#=> myArr(c) = 3
Using access the global namespace
using ::
:
proc bar {} {
parray ::myArr
}
#=> ::myArr(a) = 1
#=> ::myArr(b) = 2
#=> ::myArr(c) = 3
You can also use upvar
and uplevel
, they might or might not be easier to understand depending on your experience with Tcl; they require you to understand levels (or stack frames). Basically, the global namespace is at level 0, or #0, and each time you go into a proc
, you go deeper by 1 level.