proc global problem - I'm completely stumped 
Author Message
 proc global problem - I'm completely stumped

Here is the code:

for {set i 1} {$i <= 5} {incr i} {
  lappend pollVarList answer$i
  lappend pollIntegerVarList answerID$i
 }

 lappend pollIntegerVarList pollID hasPassedPoll willEditPoll
hasEditedPoll willDeletePoll willUndeletePoll showEntry
 lappend pollVarList question origQuestion

 set pollVarList [concat $pollVarList $pollIntegerVarList]

 foreach var $pollVarList {
  if {[catch {cgi_import $var} trash] && [lsearch -exact
$pollIntegerVarList $var]} {
   set $var 0
  } elseif {[catch {cgi_import $var} trash]} {
   set $var {}
  }
 }

 proc squashPollAnswers {index} {
  for {set i 1} {$i <= $index} {incr i} {
   global [set answer$i]
   lappend answerList [set answer$i]
  }
  set i 1; set j 1
  while {$i <= $index} {
   if {[string length [lindex $answerList [expr {$i - 1}]]] > 0} {
    set answer$j [lindex $answerList [expr {$i - 1}]]
    incr j
   }
   incr i
  }
  while {[expr {$j - 1}] < $index} {set answer$j {}; incr j}
 }

I am receiving the following error upon execution of this script:

can't read "answer1": no such variable while executing "set answer$i"
(procedure "squashPollAnswers" line 3) invoked from within
"squashPollAnswers 5" (file "val_cma/poll.tcl" line 54) invoked from
within "source "[file join $valCMAPath ${menuItem}.tcl]"" invoked from
within "if {$includeSources} { set valCMAPath val_cma lappend
menuItemList poll trivia article fortune feedback news board lappend
menuItemList features li..." (file
"/users/ppowell/web/cgi-bin/cma_main.cgi" line 80)

How is this possible? I am using [global [set answer$i]] for i = 1 to
5 and answer1 - answer 5 are set physically before the proc is evoked.

Um, HELP!

Phil



Fri, 25 Nov 2005 21:56:56 GMT  
 proc global problem - I'm completely stumped

Quote:
>  proc squashPollAnswers {index} {
>   for {set i 1} {$i <= $index} {incr i} {
>    global [set answer$i]
>    lappend answerList [set answer$i]
>   }

try:
   global answer$i

--Fred



Fri, 25 Nov 2005 22:14:13 GMT  
 proc global problem - I'm completely stumped

Quote:
> Here is the code:
> [...]
> proc squashPollAnswers {index} {
> for {set i 1} {$i <= $index} {incr i} {
> global [set answer$i]
> lappend answerList [set answer$i]
> }
> [...]
> I am receiving the following error upon execution of this script:

> can't read "answer1": no such variable while executing "set answer$i"
> (procedure "squashPollAnswers" line 3) invoked from within
> "squashPollAnswers 5" (file "val_cma/poll.tcl" line 54) invoked from
> within "source "[file join $valCMAPath ${menuItem}.tcl]"" invoked from
> within "if {$includeSources} { set valCMAPath val_cma lappend
> menuItemList poll trivia article fortune feedback news board lappend
> menuItemList features li..." (file
> "/users/ppowell/web/cgi-bin/cma_main.cgi" line 80)

> How is this possible? I am using [global [set answer$i]] for i = 1 to
> 5 and answer1 - answer 5 are set physically before the proc is evoked.

> Um, HELP!

> Phil

Hi Phil,

When i=1 then [set answer$i] will return the value of answer1
because $i is substituted before the command is run.
You could just do:
 global answer$i
 lappend aswerList answer$i

Note the following transcript:
  (bin) 103 % set i 1
  1
  (bin) 104 % set answer$i
  can't read "answer1": no such variable
  (bin) 105 % set answer1 "somestring"
  somestring
  (bin) 106 % set answer$i
  somestring
  (bin) 107 %

Best Regards,
Lars Eirik



Fri, 25 Nov 2005 22:12:35 GMT  
 proc global problem - I'm completely stumped

Quote:

>  proc squashPollAnswers {index} {
>   for {set i 1} {$i <= $index} {incr i} {
>    global [set answer$i]
>    lappend answerList [set answer$i]
>   }
>   set i 1; set j 1
>   while {$i <= $index} {
>    if {[string length [lindex $answerList [expr {$i - 1}]]] > 0} {
>     set answer$j [lindex $answerList [expr {$i - 1}]]
>     incr j
>    }
>    incr i
>   }
>   while {[expr {$j - 1}] < $index} {set answer$j {}; incr j}
>  }

> I am receiving the following error upon execution of this script:

> can't read "answer1": no such variable while executing "set answer$i"

Consider this line of code:

     global [set answer$i]

The first time through the loop i will be 1. Thus, this line is
identical to:

     global $answer1

is that what you intend? Rarely is it necessary to use a $ as part of a
global statement. The error message is telling you that there is no
variable named "answer1" at this point in the proc, which is a true
statement.

You are doing the eqivalent of this:

     set foo "variable1"
     set bar "variable2"
     proc example {index} {
         global $foo
         global $bar
     ]

Does that make the problem any more apparent?



Fri, 25 Nov 2005 22:23:03 GMT  
 proc global problem - I'm completely stumped
Others have answered your immediate question.
My additional suggestion is to abandon generating
variable names on the fly and just use an array.
The resulting code will be simpler and more robust.

You will be able to use buiilt-in array functionality
to track and query the variables too.

Roy



Fri, 25 Nov 2005 23:28:24 GMT  
 proc global problem - I'm completely stumped

Quote:


>> proc squashPollAnswers {index} {
[...]
>>    for {set i 1} {$i <= $index} {incr i} {
>>       global [set answer$i]
>>       lappend answerList [set answer$i]
>>    }
[...]
>> }
> When i=1 then [set answer$i] will return the value of answer1
> because $i is substituted before the command is run.
> You could just do:
>  global answer$i
>  lappend aswerList answer$i

However, if you are going to use generated variable names (and Tcl's arrays are
a reasonable alternative to this, as other people have suggested) then it is far
easier to work with them in a procedure if you use the [upvar] command to give
the variables (locally) static names, something like this:

  upvar #0 answer$i answer
  lappend answerList $answer

The doesn't work so well for the specific case of generating the variable names
in a loop though (you can't retarget an [upvar] variable link, IIRC) but there
is yet another way to do it, using qualified variable names:

  lappend answerList [set ::answer$i]

By leaving out the [global] (and [upvar]) command altogether, the code is
actually simpler here.  But *really* consider arrays instead.  Please.

Donal.
--

make: *** No rule to make target `war'.  Stop.



Sun, 27 Nov 2005 17:10:47 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. expect u question - i'm stumped - completely...

2. Problems installing pythonwin - I'm stumped

3. Assignment of value to a global variable inside a proc

4. Getting proc to recognize all global variables

5. Help modifying global array within a proc - oops!

6. Help modifying global array within a proc

7. Help creating global array from inside proc

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

9. Creat global variable in a proc

10. un-'upvar'ing/un-'global'ing variables

11. Tk make test doesn't completely work

12. An AWK poser that's stumped a novice

 

 
Powered by phpBB® Forum Software