Assignment of value to a global variable inside a proc 
Author Message
 Assignment of value to a global variable inside a proc

Hi all,
        I encouter this problem which i have detailed below.
I have a variable state which is defined globally
set state 0

I have two procs which use this and many other global variables
eg : proc foo{} {
       global state a b c

       set temp = 3
       if {temp > 2} {
       set $state 1
       puts "state value is now changed to $state"
       }

when i call this proc , the value remains at 0 instead of being
modified to one.
Can anyone point out my mistake here ?
Thanks in advance
Regs
tamu



Sun, 28 Sep 2008 12:15:22 GMT  
 Assignment of value to a global variable inside a proc
Hi all,
        I encouter this problem which i have detailed below.
I have a variable state which is defined globally
set state 0

I have two procs which use this and many other global variables
eg : proc foo{} {
       global state a b c

       set temp  3
       if {temp > 2} {
       set $state 1
       puts "state value is now changed to $state"
       }

when i call this proc , the value remains at 0 instead of being
modified to one.
Can anyone point out my mistake here ?
Thanks in advance
Regs
tamu



Sun, 28 Sep 2008 12:15:43 GMT  
 Assignment of value to a global variable inside a proc


Quote:
> Hi all,
>         I encouter this problem which i have detailed below.
> I have a variable state which is defined globally
> set state 0

> I have two procs which use this and many other global variables
> eg : proc foo{} {
>        global state a b c

>        set temp  3
>        if {temp > 2} {
>        set $state 1
>        puts "state value is now changed to $state"
>        }

> when i call this proc , the value remains at 0 instead of being
> modified to one.
> Can anyone point out my mistake here ?
> Thanks in advance
> Regs
> tamu

I do the same thing every once in a while.  Your set statement is incorrect.

set $state 1   This really means set 0 1 since you are deferencing the
variable state.

Use: set state 1

Personally, I do not like the "global" command since it makes your vars
ambigious as to whether they are local or global.  I use the :: global
operator on vars, so you can remove the global statement from the proc and
use:

set ::state 1

Now you immediately know you are dealing with a global var.



Sun, 28 Sep 2008 12:40:02 GMT  
 Assignment of value to a global variable inside a proc
Dear Mr.Greida,
                 Thanks a million for your suggestion and prompt reply.
It works !
You made my day

Regds
Tamu



Sun, 28 Sep 2008 14:36:42 GMT  
 Assignment of value to a global variable inside a proc
Quote:

> Hi all,

Good morning,

Quote:
>         I encouter this problem which i have detailed below.
> I have a variable state which is defined globally
> set state 0

> I have two procs which use this and many other global variables
> eg : proc foo{} {
>        global state a b c

>        set temp  3
>        if {temp > 2} {
>        set $state 1

-------------^
you are setting the variable "0" ( content of state and in local scope) to 1
this is arguably not what you want. replace with:

        set state 1

Quote:
>        puts "state value is now changed to $state"
>        }

> when i call this proc , the value remains at 0 instead of being
> modified to one.
> Can anyone point out my mistake here ?
> Thanks in advance
> Regs
> tamu

uwe


Sun, 28 Sep 2008 16:04:55 GMT  
 Assignment of value to a global variable inside a proc
Quote:

> Personally, I do not like the "global" command since it makes your vars
> ambigious as to whether they are local or global.  I use the :: global
> operator on vars

[...]

One good reason for using [global] is that it works by making a local
variable that is [upvar]ed to the global variable. This gives you the
speed of a local variable (apart from that call to [global] of course)
with the visibility and persistence of a global variable.

But the first form of optimization is *always* "Make the code work!"

Donal.



Sun, 28 Sep 2008 23:32:40 GMT  
 Assignment of value to a global variable inside a proc

Quote:
> proc foo{} {
>        global state a b c

>        set temp = 3
>        if {temp > 2} {
> #       set $state 1
        set state 1
>        puts "state value is now changed to $state"
>        }

> when i call this proc , the value remains at 0 instead of being
> modified to one.
> Can anyone point out my mistake here ?

Yes, yes I can.

Dan "Terse" Smart



Mon, 29 Sep 2008 07:20:11 GMT  
 Assignment of value to a global variable inside a proc
Am Tue, 11 Apr 2006 21:15:22 -0700 schrieb tamu_help:

Quote:
> Hi all,
>         I encouter this problem which i have detailed below.
> I have a variable state which is defined globally
> set state 0

> I have two procs which use this and many other global variables
> eg : proc foo{} {
>        global state a b c

>        set temp = 3
>        if {temp > 2} {
>        set $state 1
>        puts "state value is now changed to $state"
>        }

> when i call this proc , the value remains at 0 instead of being
> modified to one.
> Can anyone point out my mistake here ?
> Thanks in advance
> Regs
> tamu

Hi Tamu,

I don't believe

Quote:
> when i call this proc , the value remains at 0 instead of being
> modified to one.

It should produce an error:
syntax error in expression "temp > 2": variable references require
preceding $

You should add a $-sign in the if line and remove it in the set line:

Quote:
>        set temp = 3
>        if {$temp > 2} {
             ^
>        set state 1
             ^
>        puts "state value is now changed to $state"
>        }

Kind regards

Ulrich



Mon, 29 Sep 2008 08:13:36 GMT  
 Assignment of value to a global variable inside a proc

Quote:

> Am Tue, 11 Apr 2006 21:15:22 -0700 schrieb tamu_help:

>>Hi all,
>>        I encouter this problem which i have detailed below.
>>I have a variable state which is defined globally
>>set state 0

>>I have two procs which use this and many other global variables
>>eg : proc foo{} {
>>       global state a b c

>>       set temp = 3
>>       if {temp > 2} {
>>       set $state 1
>>       puts "state value is now changed to $state"
>>       }

>>when i call this proc , the value remains at 0 instead of being
>>modified to one.
>>Can anyone point out my mistake here ?
>>Thanks in advance
>>Regs
>>tamu

> Hi Tamu,

> I don't believe

>>when i call this proc , the value remains at 0 instead of being
>>modified to one.

> It should produce an error:
> syntax error in expression "temp > 2": variable references require
> preceding $

> You should add a $-sign in the if line and remove it in the set line:

>>       set temp = 3
>>       if {$temp > 2} {

>              ^

Actually, before that he should have gotten an error on the set statement.

--
Bryan Oakley
http://www.tclscripting.com



Mon, 29 Sep 2008 09:41:52 GMT  
 Assignment of value to a global variable inside a proc
Am Thu, 13 Apr 2006 01:41:52 +0000 schrieb Bryan Oakley:

Quote:

>> Am Tue, 11 Apr 2006 21:15:22 -0700 schrieb tamu_help:

>>>Hi all,
>>>        I encouter this problem which i have detailed below.
>>>I have a variable state which is defined globally
>>>set state 0

>>>I have two procs which use this and many other global variables
>>>eg : proc foo{} {
>>>       global state a b c

>>>       set temp = 3
>>>       if {temp > 2} {
>>>       set $state 1
>>>       puts "state value is now changed to $state"
>>>       }

>>>when i call this proc , the value remains at 0 instead of being
>>>modified to one.
>>>Can anyone point out my mistake here ?
>>>Thanks in advance
>>>Regs
>>>tamu

>> Hi Tamu,

>> I don't believe

>>>when i call this proc , the value remains at 0 instead of being
>>>modified to one.

>> It should produce an error:
>> syntax error in expression "temp > 2": variable references require
>> preceding $

>> You should add a $-sign in the if line and remove it in the set line:

>>>       set temp = 3
>>>       if {$temp > 2} {

>>              ^

> Actually, before that he should have gotten an error on the set statement.

Wow, yes, I overlooked it completely. But the 2 lines above look ok :-)


Mon, 29 Sep 2008 17:04:35 GMT  
 Assignment of value to a global variable inside a proc


Quote:

said:
> > proc foo{} {
> >        global state a b c

> >        set temp = 3
> >        if {temp > 2} {
> > #       set $state 1

This is interesting.  On my newsreader, Outlook Express using an Earthlink
news server, this is the first post with "set temp = 3" versus the original
"set temp 3" post.  However, (temp > 2) is also in the original post.  I
missed that.  Anyway, is a post missing on the Earthlink news server that
explains why "set temp = 3" is there?


Mon, 29 Sep 2008 17:17:02 GMT  
 Assignment of value to a global variable inside a proc

Quote:

> This is interesting.  On my newsreader, Outlook Express using an Earthlink
> news server, this is the first post with "set temp = 3" versus the original
> "set temp 3" post.  However, (temp > 2) is also in the original post.  I
> missed that.  Anyway, is a post missing on the Earthlink news server that
> explains why "set temp = 3" is there?

there where two posting by tamu_help:


Date: 11 Apr 2006 21:15:22 -0700
        set temp = 3


Date: 11 Apr 2006 21:15:43 -0700
        set temp  3

uwe, still scratching his head and cleaning his specs



Mon, 29 Sep 2008 17:24:17 GMT  
 Assignment of value to a global variable inside a proc


Quote:

> > This is interesting.  On my newsreader, Outlook Express
> > using an Earthlink news server, this is the first post
> > with "set temp = 3" versus the original "set temp 3" post.
> > However, (temp > 2) is also in the original post.  I
> > missed that.  Anyway, is a post missing on the Earthlink
> > news server that explains why "set temp = 3" is there?
> there where two posting by tamu_help:


> Date: 11 Apr 2006 21:15:22 -0700
>         set temp = 3


> Date: 11 Apr 2006 21:15:43 -0700
>         set temp  3

> uwe, still scratching his head and cleaning his specs

Thanks.  Apparently, self-censoring on the Internet does not completely
work.


Mon, 29 Sep 2008 19:48:39 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Help creating global array from inside proc

2. TCL SIGSEGV when accessing traced global var from inside a proc

3. Entry default values inside a proc

4. xotcl accessing member variable from inside a tcl proc

5. Getting proc to recognize all global variables

6. Creat global variable in a proc

7. Setting return value from proc into variable

8. How to make the default value for a proc arg equal to a variable

9. HELP: changing variable value from menu in lower proc

10. Global variable - change default value and save

11. getting values of global variables?

12. Get Global Variable Value from C++ function?

 

 
Powered by phpBB® Forum Software