View previous topic :: View next topic
|
Author |
Message |
lakshmibala
New User
Joined: 16 Jun 2004 Posts: 47
|
|
|
|
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 |
|
Back to top |
|
|
imvs
New User
Joined: 12 May 2004 Posts: 33
|
|
|
|
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 |
|
|
lakshmibala
New User
Joined: 16 Jun 2004 Posts: 47
|
|
|
|
hi
thanks a lot for the immediate reply
can you suggest some other way?-if u know
lovingly lakshmi ] |
|
Back to top |
|
|
DIVAKAR
New User
Joined: 26 Jun 2004 Posts: 3 Location: Trichy
|
|
|
|
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 |
|
|
DIVAKAR
New User
Joined: 26 Jun 2004 Posts: 3 Location: Trichy
|
|
|
|
<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 |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
An experienced pgmr would have asked: are the records sorted in sequence? The optimal solution depends on the answer.
Regards, Jack. |
|
Back to top |
|
|
lakshmibala
New User
Joined: 16 Jun 2004 Posts: 47
|
|
|
|
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 |
|
Back to top |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
Before an answer can be given you have to answer the ques: are the records in sequence? |
|
Back to top |
|
|
saravana iyappan
New User
Joined: 31 Mar 2004 Posts: 6 Location: Seattle
|
|
|
|
AFAIk the records are not in sequence.
Let me know MMwife whether the ans is right or not |
|
Back to top |
|
|
mmwife
Super Moderator
Joined: 30 May 2003 Posts: 1592
|
|
|
|
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 |
|
|
sandip_datta
Active User
Joined: 02 Dec 2003 Posts: 150 Location: Tokyo, Japan
|
|
|
|
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 |
|
|
|