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!

Interesting Query about arrays

Status
Not open for further replies.

6839470

Programmer
Mar 7, 2004
45
US
Hi,
I have a peculiar problem I am storing all my values in an array say tx($counter) where counter is set from 1 to max. now when I try this with if condition say

if {$something <= 20} {
set counter 1
set tx($counter) $x
incr counter
} else {
set fileid [open c:/command w+]
for {set counter 1} {$counter <= 1000} {incr counter} {
puts $fileid "$tx($counter)"
}
close $fileid
}

the thing is $tx($counter) is not been printed?....what must be the problem.i guess the local variable tx($counter) is been destroyed before else part.how to solve this problem.
thanks
 
I suppose that the code showed is inside a proc. If so, the tx variable is a local variable and don't remain between calls.
If you want your tx array be a static variable, you need to declare it as a global variable or as a namespace variable.
Making tx a global variable is just matter of adding :: before tx:
Code:
  proc someproc {someargs}   {
    if {$something <= 20}     {
      set counter 1
      set ::tx($counter) $x
      incr counter
    }     else     {
      set fileid [open c:/command w+]
      for {set counter 1} {$counter <= 1000} {incr counter}                  {
        puts $fileid "$::tx($counter)"
      }
      close $fileid
    }
  }
Alternatively you can put global tx at the first line of the proc and go ahead with tx.

HTH

ulis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top