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



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

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

Postby dexik » Wed Aug 08, 2012 5:36 am

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. 
dexik
 
Posts: 28
Joined: Tue Jun 12, 2012 4:30 am
Has thanked: 0 time
Been thanked: 0 time

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

 

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

Postby Robert Sample » Wed Aug 08, 2012 6:43 am

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
Robert Sample
Global moderator
 
Posts: 3367
Joined: Sat Dec 19, 2009 8:32 pm
Location: East Dubuque, Illinois
Has thanked: 1 time
Been thanked: 222 times

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

Postby dick scherrer » Wed Aug 08, 2012 6:47 am

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.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6304
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 91 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post