Getting the value of a var in a list of variables 
Author Message
 Getting the value of a var in a list of variables

I've got something like this:

set var1 100
set var2 200
set var3 300
set var4 400
set var5 500

set varList [list var1 var2 var3 var4 var5]
foreach var $varList {
    if {$$var == 400} {
        #yadda...yadda...yadda...
    }

Quote:
}

I know the $$var part is wrong but is something like what I want to do.
Is there a way for me to access the value in say var4 from the level of the
list.

Thanks
Chris



Fri, 11 Apr 2003 03:00:00 GMT  
 Getting the value of a var in a list of variables
Try:
        if {[set $var] == 400}
Quote:

> I've got something like this:

> set var1 100
> set var2 200
> set var3 300
> set var4 400
> set var5 500

> set varList [list var1 var2 var3 var4 var5]
> foreach var $varList {
>     if {$$var == 400} {
>         #yadda...yadda...yadda...
>     }
> }

> I know the $$var part is wrong but is something like what I want to do.
> Is there a way for me to access the value in say var4 from the level of the
> list.

> Thanks
> Chris



Fri, 11 Apr 2003 03:00:00 GMT  
 Getting the value of a var in a list of variables
Chris,

You can force substitution with subst.  You  might try:

  if {[subst \$$var] == 400} {
        puts "found a variable $var = 400"
    }

Vince


Quote:
> I've got something like this:

> set var1 100
> set var2 200
> set var3 300
> set var4 400
> set var5 500

> set varList [list var1 var2 var3 var4 var5]
> foreach var $varList {
>     if {$$var == 400} {
>         #yadda...yadda...yadda...
>     }
> }

> I know the $$var part is wrong but is something like what I want to do.
> Is there a way for me to access the value in say var4 from the level of
the
> list.

> Thanks
> Chris



Fri, 11 Apr 2003 03:00:00 GMT  
 Getting the value of a var in a list of variables

Quote:

> I've got something like this:
> set var1 100
> set var2 200
> set var3 300
> set var4 400
> set var5 500
> set varList [list var1 var2 var3 var4 var5]
> foreach var $varList {
>     if {$$var == 400} {
>         #yadda...yadda...yadda...
>     }
> }

> I know the $$var part is wrong but is something like what I want to do.
> Is there a way for me to access the value in say var4 from the level of the
> list.

>  Try:
>         if {[set $var] == 400}

In *most* cases, the Tcl Way is to use an array:

        set array(1) 100
        set array(2) 200
        set array(3) 300
        # or:
        array set array {
               1    100
               2    200
               3    300
        }
        foreach indx { 1 2 3 } {
               puts $array($indx)
        }

or, if all else fails, an upvar:

    set fred 1
    set barney 2
    set wilma 3
    set betty 4
    set vlist [list fred barney wilma betty]
    foreach vname $vlist {
        upvar 0 $vname var
        puts $var
    }

Note, though, that it's better to break the loop body into a proc:
proc doIt { vName } {
    upvar 1 $vName var
    puts $var

Quote:
}

in case vlist contains 'vname' or 'var' -- otherwise things get
weird.

// Kevin



Fri, 11 Apr 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. list[] = var => list.append(var) (phpstyle)

2. Getting awk computed value into shell variable

3. getting environment variable values in 1.5.2

4. getting values of global variables?

5. Float variable values getting mangled up

6. CGI getting multiple values with same name into a list

7. Getting a value back from a list box

8. session var and variable variable

9. Expanding a variable into a value list ( for tk_optionMenu )

10. Getting var=system() working?

11. Getting a pointer to a var in classic rexx

12. ?????????? value of real var. in LINUX IFC

 

 
Powered by phpBB® Forum Software