输入文件的记录为:8712351,8712353,8712353,8712354,8712356,8712352,8712355 8712352,8712355
使用COBOL,我需要从上述文件中删除重复项并写入输出文件。我编写了简单的逻辑来读取记录并写入输出文件。
我需要将删除上述文件中的重复项(例如 8712353、8712352)的逻辑放在哪里?
下面是程序逻辑:
IDENTIFICATION DIVISION.
PROGRAM-ID.RemoveDup.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUTFILEDUP ASSIGN TO 'C:\Cobol\INPUTFILEDUP.txt'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUTFILEDUP ASSIGN TO 'C:\Cobol\OUTFILEDUP.txt'
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUTFILEDUP.
01 INPUTFILEDUPREC.
88 EOFINPUTFILEDUP VALUE HIGH-VALUES.
02 INPUTFILEID PIC 9(07).
FD OUTFILEDUP.
01 OUTFILEDUPREC PIC 9(07).
WORKING-STORAGE SECTION.
77 WS-VARIABLE PIC 9(09).
77 REC-NOT-MATCH PIC 9(01).
77 CUR-VARIABLE PIC 9(09).
PROCEDURE DIVISION.
BEGIN.
OPEN INPUT INPUTFILEDUP
OPEN OUTPUT OUTFILEDUP
READ INPUTFILEDUP
AT END SET EOFINPUTFILEDUP TO TRUE
END-READ
PERFORM UNTIL (EOFINPUTFILEDUP)
WRITE OUTFILEDUPREC FROM INPUTFILEID
READ INPUTFILEDUP
AT END SET EOFINPUTFILEDUP TO TRUE
PERFORM UNTIL (EOFINPUTFILEDUP)
END-READ
END-PERFORM
CLOSE INPUTFILEDUP
CLOSE OUTFILEDUP
STOP RUN.
我将 tnput 文件按升序排序为:
8712351,8712353,8712353,8712354,8712356,8712352,8712355,8712352,8712355
它起作用了,下面是修改后的代码:
但是假设如果我的文件不是升序或降序,那么我需要在删除重复项之前编写排序逻辑。如何为此更新以下代码?正如我所尝试的,但如果输入文件结构如下所示,我没有成功执行此操作:
8712351,8712353,8712353,8712354,8712356,8712352,8712355,8712352,8712355
IDENTIFICATION DIVISION.
PROGRAM-ID.RemoveDup2.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUTFILEDUP ASSIGN TO 'C:\Cobol\INPUTFILEDUP.txt'
ORGANIZATION IS LINE SEQUENTIAL.
SELECT OUTFILEDUP ASSIGN TO 'C:\Cobol\OUTFILEDUP.txt'
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUTFILEDUP.
01 INPUTFILEDUPREC.
88 EOFINPUTFILEDUP VALUE HIGH-VALUES.
02 INPUTFILEID PIC 9(07).
FD OUTFILEDUP.
01 OUTFILEDUPREC PIC 9(07).
WORKING-STORAGE SECTION.
77 WS-VARIABLE PIC 9(09) VALUE ZERO.
77 REC-NOT-MATCH PIC 9(01).
77 CUR-VARIABLE PIC 9(7) VALUE ZERO.
PROCEDURE DIVISION.
BEGIN.
OPEN INPUT INPUTFILEDUP
OPEN OUTPUT OUTFILEDUP
READ INPUTFILEDUP
AT END SET EOFINPUTFILEDUP TO TRUE
END-READ
PERFORM UNTIL (EOFINPUTFILEDUP)
IF INPUTFILEID NOT EQUAL TO WS-VARIABLE
MOVE INPUTFILEID TO WS-VARIABLE
WRITE OUTFILEDUPREC FROM INPUTFILEID
READ INPUTFILEDUP
AT END SET EOFINPUTFILEDUP TO TRUE
PERFORM UNTIL (EOFINPUTFILEDUP)
ELSE
DISPLAY "dUPLICATE FOUND" INPUTFILEID
READ INPUTFILEDUP
AT END SET EOFINPUTFILEDUP TO TRUE
END-READ
END-PERFORM
CLOSE INPUTFILEDUP
CLOSE OUTFILEDUP
STOP RUN.