Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

declare global array

1,054 views
Skip to first unread message

KAN

unread,
Jun 15, 1994, 6:15:37 PM6/15/94
to
Hi,

I am a new user of tcl. I am having some trouble with arrays.

The following is a test program:

global a
proc parray {} {
set a(0) 11
set a(1) 22
}

parray

After I ran the procedure parray, I tried to access a(0) and a(1)
by typing:

set a(0)

I got an error message of "can't read "a(0)": no such variable

I also tried to declare the array a in the following form:

global a()

It did not work either.
Could someone help me how can I declare a global array and be able
to access it at any part of the program?
Thanks in advance.

Lili Kan

lil...@cory.eecs.berkeley.edu
k...@adtaz.sps.mot.com

Michael Salmon

unread,
Jun 16, 1994, 3:16:26 AM6/16/94
to
In article <2tnui9$7...@agate.berkeley.edu>, lil...@cory.EECS.Berkeley.EDU (KAN) writes:
|> Hi,
|>
|> I am a new user of tcl. I am having some trouble with arrays.
|>
|> The following is a test program:
|>
|> global a
|> proc parray {} {
|> set a(0) 11
|> set a(1) 22
|> }
|>
|> parray
|>
|> After I ran the procedure parray, I tried to access a(0) and a(1)
|> by typing:
|>
|> set a(0)
|>
|> I got an error message of "can't read "a(0)": no such variable
|>
|> I also tried to declare the array a in the following form:
|>
|> global a()
|>
|> It did not work either.
|> Could someone help me how can I declare a global array and be able
|> to access it at any part of the program?
|> Thanks in advance.

It needs to be declared global in each procedure.

--

Michael Salmon

#include <standard.disclaimer>
#include <witty.saying>
#include <fancy.pseudo.graphics>

Ericsson Telecom AB
Stockholm

Dominik Zimmermann

unread,
Jun 16, 1994, 4:53:54 AM6/16/94
to
In article <2tnui9$7...@agate.berkeley.edu>, lil...@cory.EECS.Berkeley.EDU (KAN) writes:
> Hi,
>
> I am a new user of tcl. I am having some trouble with arrays.
>
> The following is a test program:
>
> global a
> proc parray {} {
> set a(0) 11
> set a(1) 22
> }
>
> parray
>
> After I ran the procedure parray, I tried to access a(0) and a(1)
> by typing:
>
> set a(0)
>
> I got an error message of "can't read "a(0)": no such variable
>
> I also tried to declare the array a in the following form:
>
> global a()
>
> It did not work either.
> Could someone help me how can I declare a global array and be able
> to access it at any part of the program?

To access a global variable inside a procedure you need to declare it global
inside the procedure like this:

proc foo {} {

global someVar

[...]

}

To *modify* a global variable inside a procedure you need to use the upvar
command:

proc foo {} {

upvar someVar x

set x someValue

[...]

}

Check the man page for details.

Cheers,

Dominik

-----------------------------------------------------------------------------
Dominik Zimmermann Technische Universitaet Berlin
Sekr. 5-6
email: dom...@cs.tu-berlin.de Franklinstr. 28/29
ph: +49-30-314-21762 10587 Berlin
Germany

Some people believe that the stars determine their fate;
I believe in the One who made the stars.

0 new messages