IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

eliminating duplicate records


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
lakshmibala

New User


Joined: 16 Jun 2004
Posts: 47

PostPosted: Wed Jun 16, 2004 4:14 pm
Reply with quote

hi everybody

this was an interview question asked in ibm.
1.write a program to eliminate duplicate records in COBOL and not in JCL.
please write a program.]

bye
lakshmi icon_rolleyes.gif
Back to top
View user's profile Send private message
imvs

New User


Joined: 12 May 2004
Posts: 33

PostPosted: Wed Jun 16, 2004 10:48 pm
Reply with quote

lakshmibala,

Here is the required code

IDENTIFICATION DIVISION.
PROGRAM-ID. DUP.
****************************************************
* *
* TO ELIMINATE DUPLICATE RECORDS *
* *
* *
****************************************************

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT IN-FILE ASSIGN TO INDD
FILE STATUS IS IN-STATUS.
SELECT OUT-FILE ASSIGN TO OUTDD
FILE STATUS IS OUT-STATUS.

DATA DIVISION.
FILE SECTION.
FD IN-FILE.
01 IN-REC.
02 FS-FIELD-1 PIC X.
FD OUT-FILE.
01 OUT-REC PIC X.

WORKING-STORAGE SECTION.
01 WS-VARS.
02 IN-STATUS PIC XX.
02 OUT-STATUS PIC XX.
02 EOF-SW PIC X VALUE 'N'.
88 NO-MORE-RECS VALUE 'Y'.
88 MORE-RECS VALUE 'N'.
02 FIND-SW PIC X VALUE 'N'.
88 FOUND VALUE 'Y'.
88 NOT-FOUND VALUE 'N'.
02 F-TBL OCCURS 1 TO 99 TIMES
DEPENDING ON RECD-CNTR
INDEXED BY IDX.
03 WS-FIELD-1 PIC X.
02 RECD-CNTR PIC 99 VALUE ZERO.
02 WS-DUM PIC 9.

PROCEDURE DIVISION.
000-PARA.
INITIALIZE WS-VARS.
OPEN INPUT IN-FILE.
IF IN-STATUS NOT = '00'
DISPLAY 'ERROR IN OPENING'
STOP RUN
END-IF.

READ IN-FILE
AT END SET NO-MORE-RECS TO TRUE
END-READ
IF IN-STATUS = '00'
ADD 1 TO RECD-CNTR
SET IDX TO 1
MOVE FS-FIELD-1 TO WS-FIELD-1(IDX)
END-IF

PERFORM UNTIL NO-MORE-RECS
READ IN-FILE
AT END SET NO-MORE-RECS TO TRUE
END-READ
IF IN-STATUS = '00'
* PERFORM SEARCH-PARA
SET IDX TO 1
SET NOT-FOUND TO TRUE

SEARCH F-TBL
AT END
SET NOT-FOUND TO TRUE
WHEN WS-FIELD-1(IDX) = FS-FIELD-1
SET FOUND TO TRUE
END-SEARCH

IF NOT-FOUND
ADD 1 TO RECD-CNTR
SET IDX TO RECD-CNTR
MOVE FS-FIELD-1 TO WS-FIELD-1(IDX)
END-IF
END-IF
END-PERFORM.

CLOSE IN-FILE.
OPEN OUTPUT OUT-FILE.

IF OUT-STATUS NOT = '00'
DISPLAY 'ERROR IN OPENING '
STOP RUN
END-IF.

PERFORM VARYING IDX FROM 1 BY 1 UNTIL IDX > RECD-CNTR
DISPLAY WS-FIELD-1(IDX)
MOVE WS-FIELD-1(IDX) TO OUT-REC
WRITE OUT-REC
END-PERFORM.
STOP RUN.


SEARCH-PARA.
SET IDX TO 1.
SET NOT-FOUND TO TRUE.
PERFORM VARYING IDX FROM 1 BY 1 UNTIL IDX > RECD-CNTR
OR FOUND
IF WS-FIELD-1(IDX) = FS-FIELD-1
SET FOUND TO TRUE
END-IF
END-PERFORM.
Back to top
View user's profile Send private message
lakshmibala

New User


Joined: 16 Jun 2004
Posts: 47

PostPosted: Thu Jun 17, 2004 11:15 am
Reply with quote

hi
thanks a lot for the immediate reply
can you suggest some other way?-if u know

lovingly lakshmi icon_biggrin.gif ]
Back to top
View user's profile Send private message
DIVAKAR

New User


Joined: 26 Jun 2004
Posts: 3
Location: Trichy

PostPosted: Sat Jun 26, 2004 5:06 am
Reply with quote

IDENTIFICATION DIVISION.
PROGRAM-ID. DUP.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SNAME. IDENTIFICATION DIVIS
05 NAME PIC X(1 PROGRAM-ID. DUP.
77 I PIC 99. ENVIRONMENT DIVISION
77 J PIC 99. DATA DIVISION.
77 N PIC 99. WORKING-STORAGE SECT
77 K PIC 99. 01 SNAME.
77 T PIC 99. 05 NAME PIC X(10)
77 I1 PIC 99.
77 T1 PIC 99.
PROCEDURE DIVISION.
MAIN-PARA.
MOVE 10 TO N.
PERFORM READ-PARA VARYING I FROM 1 BY 1 UNTIL I > N.
DISPLAY ' BEFORE ELIMINATION '.
PERFORM DISP-PARA VARYING I FROM 1 BY 1 UNTIL I > N.
DISPLAY ' '.
SUBTRACT 1 FROM N GIVING T.
PERFORM REMO-PARA VARYING I FROM 1 BY 1 UNTIL I > T.
DISPLAY ' AFTER ELIMINATION '.
PERFORM DISP-PARA VARYING I FROM 1 BY 1 UNTIL I > N.
STOP RUN.
READ-PARA.
ACCEPT NAME(I).
DISP-PARA.
DISPLAY NAME(I).
REMO-PARA.
COMPUTE K = I + 1 .
PERFORM REM1-PARA VARYING J FROM K BY 1 UNTIL J > N.
REM1-PARA.
IF NAME(I) = NAME (J)
PERFORM SHIFT-PARA VARYING I1 FROM J BY 1 UNTIL I1 > T
SUBTRACT 1 FROM T
SUBTRACT 1 FROM N
ADD 1 TO J.
SHIFT-PARA.
COMPUTE T1 = I1 + 1.
MOVE NAME(T1) TO NAME(I1).



- Dhivakar
Back to top
View user's profile Send private message
DIVAKAR

New User


Joined: 26 Jun 2004
Posts: 3
Location: Trichy

PostPosted: Sat Jun 26, 2004 5:16 am
Reply with quote

<pre>
IDENTIFICATION DIVISION.
PROGRAM-ID. DUP.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SNAME.
05 NAME PIC X(10) OCCURS 10 TIMES.
77 I PIC 99.
77 J PIC 99.
77 N PIC 99.
77 K PIC 99.
77 T PIC 99.
77 I1 PIC 99.
77 T1 PIC 99.
PROCEDURE DIVISION.
MAIN-PARA.
MOVE 10 TO N.
PERFORM READ-PARA VARYING I FROM 1 BY 1 UNTIL I > N.
DISPLAY ' BEFORE ELIMINATION '.
PERFORM DISP-PARA VARYING I FROM 1 BY 1 UNTIL I > N.
DISPLAY ' '.
SUBTRACT 1 FROM N GIVING T.
PERFORM REMO-PARA VARYING I FROM 1 BY 1 UNTIL I > T.
DISPLAY ' AFTER ELIMINATION '.
PERFORM DISP-PARA VARYING I FROM 1 BY 1 UNTIL I > N.
STOP RUN.
READ-PARA.
ACCEPT NAME(I).
DISP-PARA.
DISPLAY NAME(I).
REMO-PARA.
COMPUTE K = I + 1 .
PERFORM REM1-PARA VARYING J FROM K BY 1 UNTIL J > N.
REM1-PARA.
IF NAME(I) = NAME (J)
PERFORM SHIFT-PARA VARYING I1 FROM J BY 1 UNTIL I1 > T
SUBTRACT 1 FROM T
SUBTRACT 1 FROM N
ADD 1 TO J.
SHIFT-PARA.
COMPUTE T1 = I1 + 1.
MOVE NAME(T1) TO NAME(I1).
</pre>
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Sun Jun 27, 2004 8:49 pm
Reply with quote

An experienced pgmr would have asked: are the records sorted in sequence? The optimal solution depends on the answer.

Regards, Jack.
Back to top
View user's profile Send private message
lakshmibala

New User


Joined: 16 Jun 2004
Posts: 47

PostPosted: Mon Jun 28, 2004 12:57 pm
Reply with quote

hi all,

i need to eliminate duplicate records not from a table but from a file

i hope i'm clear

kindly suggest me some logic with codings

bye

lakshmibala icon_lol.gif
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Mon Jun 28, 2004 6:53 pm
Reply with quote

Before an answer can be given you have to answer the ques: are the records in sequence?
Back to top
View user's profile Send private message
saravana iyappan

New User


Joined: 31 Mar 2004
Posts: 6
Location: Seattle

PostPosted: Tue Jul 06, 2004 8:12 pm
Reply with quote

AFAIk the records are not in sequence.

Let me know MMwife whether the ans is right or not
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Fri Jul 09, 2004 1:26 am
Reply with quote

This is a very poorly defined problem. There are at least 3 different scenerios that can arise from the problem as defined:

1) All recs are sorted in sequence.
2) They are not sorted but the dupes immediately follow the original recs.
3) The recs are in completely random order.

The solution for #3 has been discussed.

For #1 and #2 do "read ahead" processing where you can compare the current rec to the previous.

There can be other solutions to #3 but it depends on the specific details of the problem. For example, if there are a reasonable number of recs and they contain a key value (e.g. a sequence #, acct #) that begins at 1 and proceeds to 100 or 1000 or ...

You could create a table and index move the I/P recs into the table. If a given slot is already filled you have a dupe situation.

A general comment is that any solution that requires an in core table (i.e. unsorted data) will not work for a file of any subtantial size.

Regards, Jack.
Back to top
View user's profile Send private message
sandip_datta

Active User


Joined: 02 Dec 2003
Posts: 150
Location: Tokyo, Japan

PostPosted: Fri Jul 09, 2004 8:43 am
Reply with quote

In addition to Jack, we can do this by introducing SORT file in COBOL Program as in INTERNAL SORT and then compare records with previous one. I think interview is asking for SD and Internal Sort related things.

Regards,
Sandip.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts combine multiple unique records into ... DFSORT/ICETOOL 2
No new posts To Omit records based n SORT condition DFSORT/ICETOOL 6
No new posts To fetch records that has Ttamp value... DFSORT/ICETOOL 5
No new posts ICETOOL returns no records JCL & VSAM 1
No new posts Compare 2 files and retrive records f... DFSORT/ICETOOL 3
Search our Forums:

Back to Top