Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

DO UNTIL problem

May
5
0
H

there seems to be an error with 'gosub' and 'do until'
in TCC V31.00.11 x64.
The attached file will show the problem. TCC complaines about
the syntax when running the script.

- Heinz
 

Attachments

  • f.btm
    738 bytes · Views: 8
The first error that I get is;
Code:
TCC: R:\f.btm [40]  Invalid array argument (out of bounds) "sel[%Y]"
...which needs to be addressed.

Maybe add an ON ERROR for error trapping to help with debugging.

Joe
 
That's what I also get. But this is caused by theDO UNTIL error.
What I also get is (in German) this error:
Syntax: IF [/I] [NOT] Bedingung [.AND. | .OR. | .XOR. [NOT] Bedingung ...] Befehl
 
The errors in the loop do not start until Y equals 5;
Code:
TCC: R:\f.btm [41]  Invalid array argument (out of bounds) "sel[%Y]"
R:\f.btm [41]  Usage : IF [/I] [NOT] condition [.AND. | .OR. | .XOR. [NOT] condition ...] command
found: 0
    Y: 5
   S1: -1
 Line: 50
Press any key when ready...

Revised code for error trapping;
Code:
:TST
Set found=0
Set Y=0
REM DO UNTIL does't work, DO WHILE works
DO UNTIL %found eq 1 .OR. %Y eq %S1
  Gosub Results
REM DO WHILE %found eq 0 .AND. %Y LE %S1
  IF %sel[%Y] == %N  Set found=1
  Set Y=%@INC[Y]
ENDDO
RETURN

:Results
echo found: %found
echo     Y: %Y
echo    S1: %S1
echo  Line: %_Batchline
pause
Return

Thus, the DO UNTIL works, but it stops working when Y equals 5, with the error;
Code:
TCC: R:\f.btm [41]  Invalid array argument (out of bounds) "sel[%Y]"

Joe
 
The fundamental problem I see here is that you're walking off the end of your array. Your index is set to 0, 1, 2, 3, 4, 5, 6.... and continues on until either (A) you find the value you're looking for, or (B) the index reaches -1. That second condition won't happen for a very long time....
 
The conditionals are not opposites (and I think they should be):

Code:
DO UNTIL %found == 1 .OR. %Y == %S1
REM DO WHILE %found == 0 .AND. %Y LE %S1

Try

Code:
DO UNTIL %found == 1 .OR. %Y GT %S1
 
DO WHILE and DO UNTIL are not logical opposites. DO WHILE is an entry-condition loop, DO UNTIL is an exit-condition loop. (Despite, you know, the condition being right up there at the top.)

But I wouldn't use either one for this purpose. I think DO Y = start TO end would make more sense. Just make sure both start and end are correct. If you want to quit as soon as a matching value is found, SET FOUND=1 and then LEAVE.
 
DO WHILE and DO UNTIL are not logical opposites.
Right, but if you want them to act the same except for the entry/exit difference you noted, the conditions should be logical opposites.

The first time here DO UNTIL %found == 1 .OR. %Y == %S1 Y == S1 and the body of the loop is executed. Thereafter, Y GT S1 and you can't get out of the loop (all the sel[*] being -1).

I have no idea what the BTM is supposed to do. My suggestion gets rid of the error; I don't know if it makes the BTM work correctly. With it, I get

Code:
c:\users\vefatica\desktop> f.btm
Zero
Two
 
Right, but if you want them to act the same except for the entry/exit difference you noted, the conditions should be logical opposites.

Not correcting you, Vincent. I just want to make the point, because I think the DO UNTIL syntax is confusing. The condition is written at the top of the loop, but tested at the bottom. Caveat coder.
 
I get the error straight away:
f
TCC: D:\Temp\f.btm [40] Invalid array argument (out of range) "sel[%Y]"
D:\Temp\f.btm [40] Syntax: IF [/I] [NOT] Condition [.AND. | .OR. | .XOR. [NOT] condition...] command
TCC: D:\Temp\f.btm [40] Invalid array argument (out of range) "sel[%Y]"
D:\Temp\f.btm [40] Syntax: IF [/I] [NOT] Condition [.AND. | .OR. | .XOR. [NOT] condition...] command
TCC: D:\Temp\f.btm [40] Invalid array argument (out of range) "sel[%Y]"

BTW: The invalid ARRAY index is the result of the error and therefore NOT a TCC problem! The syntax should actually be correct, but is still criticized!
 
BTW, thre is an error in the code. The first time Y can't reach S1 because S1 is initialized to -1 and
Y starts from 0 and is increasing. Found this using BDEBUGGER.
 
Now it works. The expression was wrong and therefor the loop
didn't terminate. Now the DO REPEAT line is this:

Code:
DO UNTIL %found == 1 .OR. %Y GT %S1
 
Back
Top