Page 1 of 1

How to move unique entries in an array to a new one?

PostPosted: Wed Aug 08, 2012 5:36 am
by dexik
I have an array with dates, some dates repeat. I need to move unique, non repeating dates to a new array. I am trying this piece of code and it isn't working for me. It isn't moving any dates nor zeros. When I display the new array it is empty.

   PERFORM 235-MOVE-NEW-DATE-RTN UNTIL SUB-2 > TABLE-LENGTH

    235-MOVE-NEW-DATE-RTN.

            MOVE 0 TO SUB-1.
            MOVE 2 TO SUB-2.

           PERFORM VARYING SUB-1 FROM 1 BY 1 UNTIL SUB-1 > TABLE-LENGTH

             IF RACE-DATE(SUB-1) NOT = RACE-DATE(SUB-2)

              MOVE RACE-DATE(SUB-1) TO RACE-DATE-UNIQUE(SUB-1)

             ELSE MOVE 0 TO RACE-DATE-UNIQUE(SUB-1)
             END-IF

             COMPUTE SUB-2 = SUB-2 + 1

           END-PERFORM. 

Re: How to move unique entries in an array to a new one?

PostPosted: Wed Aug 08, 2012 6:43 am
by Robert Sample
1. You don't test to see if SUB-1 equals SUB-2 so your results are always going to find a duplicate when the two are equal.

2. Lets look at the loop iterations for a moment:
First time through SUB-1 = 1, SUB-2 = 2
Assume dates are not equal, so UNIQUE (1) gets the value
Next time through, SUB-1 = 2, SUB-2 = 2
Dates are going to be equal, so UNIQUE (1) gets a zero
start to see the problem?

3. You would be MUCH better off using a flag (untested code so beware):
PERFORM VARYING SUB-1 FROM 1 BY 1 UNTIL SUB-1 > TABLE-MAX
    MOVE ZERO TO FLAG-BYTE
    PERFORM VARYING SUB-2 FROM SUB-1+1 TO TABLE-MAX
        IF   RACE-DATE (SUB-1) = RACE-DATE (SUB-2)
             MOVE 1 TO FLAG-BYTE
        END-IF
    END-PERFORM
    IF  FLAG-BYTE = ZERO
        MOVE RACE-DATE (SUB-1) TO RACE-DATE-UNIQUE (SUB-1)
    END-IF
END-PERFORM

Re: How to move unique entries in an array to a new one?

PostPosted: Wed Aug 08, 2012 6:47 am
by dick scherrer
Hello,

Is this basucally the same question as your other recent COBOL question?

Suggest you rethink how this could work and "play computer" with pen and paper to see why this doesn't work.

If you cannot figure this out after spending an hour verifying the code, post the arrays and the code that references the arrays. The code before and after the "free standing" perform also needs to be posted.