# Counter math error

I have the following from a DB with 49 weekly columns. I have a procedure that checks each column to find if any are missing. The procedure fails when it gets to WK31. I used the following to check the procedure and noted an error in counter (lxN) vs the column name.
lxN=0 axFld=""
Loop
lxN=lxN+1
axFld= axFld+¶+pattern(lxN,"## ")+info(“fieldname”)
Right
Until lxN=50

The results (truncated for clarity) are:

29 Wk29
30 Wk30
31 Wk31
33 Wk32
34 Wk33
35 Wk34

Note that the value of lxN jumps from 31 to 33, for no reason. I have tried int( and round( and get the same result.

Definately something screwy here. Even using this simplest form of code shows the problem.

``````let x=1
Loop
x=x+1
zlog x
Until x=35
``````

This produces (partial):

``````
…
[Test/Procedure_B] 28
[Test/Procedure_B] 29
[Test/Procedure_B] 30
[Test/Procedure_B] 31
[Test/Procedure_B] 33
[Test/Procedure_B] 34
[Test/Procedure_B] 35
``````

If I change the initial value of x from 0 to 1 then item 32 shows but item 33 is missing. If, however, I use this code it all works properly.

``````for i,1,35
zlog i
endloop
``````

``````axFld=""
for i,1,49
axFld= axFld+¶+pattern(i,"## ")+info(“fieldname”)
Right
end loop
``````

This is a very weird bug. It seems to happen only if the assignment statement is the first statement after the loop statement. In that case, once every 33 times Panorama jumps back up to the top of the loop after the assignment (it’s not a calculation error, it’s a program flow error). This causes the assignment to run twice. I don’t know why, and I have written this up for further investigation.

I think Gary’s solution of using a for loop is the best solution for now. But I was also able to get it to work simply by adding a NOP statement right after the loop and before the assignment.

It doesn’t have anything to do with the original question, but in Panorama 6 it was common practice to scan an array with a loop incrementing a variable, like this:

``````let n=1
loop
let element = array(theArray,n,separator)
stoploopif element=""
.... do something
n = n+1
endloop
``````

Since the assignment isn’t the first statement in the loop, this technique doesn’t trigger the bug.

Note: In Panorama X, there’s a much better way to scan an array:

``````looparray theArray,separator,element
... do something
endloop
``````

Thanks. I will try these.
I ran this out for a few hundred iterations and it appears the skipped numbers hit on or around each 32 (2^5) cycles.

just tried the following:
local lxN,axN
axN=""
lxN=1
loop
axN=axN+¶+lxN
lxN=lxN+1
until lxN=500
scrapcalc axN

now it doubles the values every 32 cycles

tried this:
for lxN,1,500
axN=axN+¶+lxN
lxN=lxN+1
endloop
scrapcalc axN

and this works

Thanks all, hope you find the bug

The assignment to increment IxN is unnecessary. All you need is one line in the loop:

``````for IxN,1,500
axN=axN+¶+IxN
endloop``````

Also found that adding an “Until formula” before the endloop works to exit the loop

axCnt=""
for lxN,1,500
axCnt=axCnt+¶+pattern(lxN,"##")
until lxN=130
scrapcalc axCnt

Sure, but there is no reason to do it that way. In fact, it will make the loop slower. Better to just put 130 instead of 500 in the for statement if that’s what you want to do, and use endloop at the bottom.