# RE: Optimisation ?

```>> -----Original Message-----
>> Behalf Of Jonathan Leckie

>> I have the following piece of code :
>>
>> IF REC<1> £ '' THEN
>>   MAX=DCOUNT(REC<1>,VM)
>>   FOR LOP=1 TO MAX
>>     IF REC<1,LOP> = '' THEN
>>       REC=DELETE(REC,1,LOP,0)
>>       IF LOP = MAX THEN GO 300
>>       LOP=LOP-1
>>     END
>>   NEXT LOP
>> END```
```
I'm just going to say this: Any code that messes with the loop variable from
a FOR loop is asking for trouble.
Pick and Prime type environments used to do their incrementing differently -
ie pre/post test.  If you want to loop a variable number of times and you
feel the need to use the loop counter then take ownership of it yourself and
use a LOOP ... WHILE ... REPEAT construct.

In this simple scenario, TRIM() is obviously better, faster, etc, but if
perhaps you had to delete other associated multivalues from other attributes
based on the value in REC<1>, or if you wanted to delete all the values
which were "TBD" lets say, instead of "", then you'd need a loop construct.

Generally, when processing dynamic arrays field by field, or attribute by
attribute it is best to work backwards as Wol suggests, but IMHO if you need
to go forward through a loop anything but a fixed number of times, you
should use LOOP ... REPEAT.

eg:

* delete the first three occurrences of "TBD" in REC<98>
* and the associated values from REC<47>, REC<99> and REC<33>
DIM WORK_ARR(4,2)
MAT WORK_ARR = ""
WORK_ARR(1,1) = 98
WORK_ARR(2,1) = 99
WORK_ARR(3,1) = 47
WORK_ARR(4,1) = 33
FOR ATT = 1 TO 4
WORK_ARR(I,2) = REC<WORK_ARR(I,1)>
NEXT ATT
LOOP_LIM = DCOUNT(WORK_ARR(1,2), @VM)
BITS_DELETED = 0
LOOP_IDX = 1
LOOP
CURR_BIT = WORK_ARR(1,2)<1,LOOP_IDX>
IF CURR_BIT = "TBD" THEN
FOR ATT = 1 TO 4
WORK_ARR(ATT,2) = DELETE(WORK_ARR(ATT,2), 1, LOOP_IDX, 0)
NEXT ATT
LOOP_LIM -= 1
LOOP_IDX -= 1
BITS_DELETED += 1
END
WHILE LOOP_IDX < LOOP_LIM AND BITS_DELETED < 3
LOOP_IDX += 1
REPEAT
FOR ATT = 1 TO 4
REC<WORK_ARR(ATT,1)> = WORK_ARR(ATT,2)
NEXT ATT
*
* don't reference ATT here since I'm outside the FOR ... NEXT,
* but OK to reference LOOP_IDX because it belongs to me!
*
CRT BITS_DELETED:" values were deleted, ":LOOP_IDX:" out of ":LOOP_LIM:"
values were tested"

Cheers,

Ken

<grrr>
PS.  Why do you think the Americans on this list felt the need to change the
spelling of "Optimisation" from the original English (Scots?).  The rest of
us put up with enough "centers" and "realizes" from you yanks!

Did anyone else get the monthly jBASE "Did you know?" email from Kerri
Duffy?  In it she says:

Did you know…?  Charles Lindbergh was not the first man to fly nonstop
across the Atlantic Ocean.  Two men had achieved the same goal eight years
earlier!   Flying for sixteen and a half hours from June 14-15, 1919,
Captain John Alcock and Lieutenant Arthur Whitten-Brown had copiloted a
Vichers-Vimy twin-engine plane nonstop from Newfoundland across the Atlantic
to Ireland.  Lindbergh was just the first person to do it alone.

She could just as easily have said "Lindbergh was just the first American to
do it".  I think most non-yanks would have known that Alcock and Brown's was
the first crossing of the Atlantic by air.

</grrr>

--
u2-users mailing list
[EMAIL PROTECTED]
http://www.oliver.com/mailman/listinfo/u2-users
```