Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DeclaringVariables

Status
Not open for further replies.

shussai2

Programmer
Aug 28, 2003
42
CA
Hi,

Actually this is my first time posting a message here. So if I do make any mistakes please bear with me.

Here is just few lines of codes first:

while {$rowIteration < [llength $addressList]} {
set m [lindex $addressList $rowIteration]
global cbox$m
set tempVar $cbox$m ///HERE IS THE PROBLEM
incr rowIteration
}

The way I have defined $cbox$m is wrong.
I want to loop and access the information stored in check boxes named cbox0, cbox1, cbox2, ... where 0 1 2.. are represented by m. I am basically appending m to end of cbox and trying to get that variable's value in tempVar.


Please help me out this really important.

Thanks


 
cbox is not a variable, it's a contstant (string).
refer to it as such:

tcl>set x 0
tcl>set y cbox$x
tcl>puts $y
cbox0

Hope this helps.
Tom
 
Given the way you've named your variables, you'll need to do it this way:

Code:
set tempVar [set cbox$m]

When you call the set command with only one argument, it returns the value currently stored in that variable.

More generally, cobbling together such dynamically-named variables is usually bad practice. More often, it's much better and easier to use Tcl arrays to accomplish the task. Tcl arrays are associative arrays, rather than integer-indexed arrays; they're the same as Awk arrays and Perl hashes, and similar to Python dictionaries. They are unordered data structures where the indices can be any string value. However, you can also use strings that look like integers if you want to iterate over them in some sequence. For more information on arrays, you might like to visit the Tcl'ers Wiki ( perhaps starting with the page &quot;array,&quot;
Anyway, here's a quick example:

Code:
set values(0) Zero
set values(1) One
set values(2) Two
set values(3) Three
set values(4) Four
set values(5) Five

proc stuff {} {
  global values

  for {set i 0} {$i <= 5} {incr i} {
    set temp $values($i)
    puts &quot;temp is $temp&quot;
  }
}

As another aside, in the code you posted above, I noticed you iterating through the elements of list using a for loop. Although that approach works, it's easier and more efficient in most cases to use Tcl's foreach loop. For more information on foreach check out the Wiki page &quot;foreach,&quot; Here's a quick example:

Code:
foreach m $addressList {
    puts &quot;The next element is: $m&quot;
}

- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
I like to thank Mr. Jones and Tom for replying to my post of DeclaringVariable. I have just begun using TCL/TK two weeks ago, therefore there are few things and conventions I am not familiar with.
Thanks alot for your tips.

Regards,

Sajjad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top