'iterator = startvalue' is executed before saving the value of 'endvalue'.
If using 'FOR NC1 = 1 TO NC1 - 1', the 'NC1' variable is set to '1' before saving the value of 'NC1 - 1' (therefore 1 - 1 = 0).
Moreover, it is not advisable to use the value of the iterator at the exit of the loop because this value ('endvalue + 1' in your case) is not an absolute requirement but rather the usual behavior for the implementation of a 'For' loop by any compiler.
For these 2 reasons, the best practice is usually to use a new variable ('I' below) for the loop iterator:
Code: Select all
' ** STORE COMMANDS **
#lang "QB"
'Screen
Color 2
' Returns the number of times that a substring is found within a string.
' ----------------------------------------------------------------------
FUNCTION fnstrn (Search$, LookFor$)
Count% = 0
IF Search$ = "" OR LookFor$ = "" THEN
fnstrn = Count%
EXIT FUNCTION
END IF
x% = INSTR(Search$, LookFor$)
DO WHILE x% > 0
Count% = Count% + 1
last% = x%
x% = INSTR((last% + 1), Search$, LookFor$)
LOOP
fnstrn = Count%
END FUNCTION
CA1$ = "A,B,C,D,F,G,H,L,M,Q,U,W"
' Count how many commands elements exist
NC1 = fnstrn(CA1$, ",") + 1
'Redim CA()
REDIM CA(NC1) AS STRING
' Store commands in array
BB = 0: EB = 0
FOR I = 1 TO NC1 - 1
EB = EB + 1: BB = EB
EB = INSTR(BB, CA1$, ",")
CA(I) = MID$(CA1$, BB, EB - BB)
Print I;"= ";CA(I)
NEXT I
CA(NC1) = MID$(CA1$, EB + 1)
Print NC1;"= ";CA(NC1)
sleep