Cobol 1:N match sample
时间: 2024-05-13 17:17:14 浏览: 110
cobol-example
5星 · 资源好评率100%
Here's a sample COBOL program for 1:N matching:
```
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT MASTER-FILE ASSIGN TO "MASTER.DAT"
ORGANIZATION IS INDEXED
ACCESS MODE IS DYNAMIC
RECORD KEY IS MASTER-KEY.
SELECT TRANSACTION-FILE ASSIGN TO "TRANSACTION.DAT"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD MASTER-FILE.
01 MASTER-RECORD.
05 MASTER-KEY PIC X(10).
05 MASTER-DATA PIC X(50).
FD TRANSACTION-FILE.
01 TRANSACTION-RECORD.
05 TRANSACTION-KEY PIC X(10).
05 TRANSACTION-DATA PIC X(50).
WORKING-STORAGE SECTION.
01 EOF-FLAG PIC X(1) VALUE 'N'.
01 MATCH-FLAG PIC X(1) VALUE 'N'.
01 MATCH-FOUND-FLAG PIC X(1) VALUE 'N'.
01 ERROR-FLAG PIC X(1) VALUE 'N'.
01 MASTER-FOUND-FLAG PIC X(1) VALUE 'N'.
01 TRANSACTION-FOUND-FLAG PIC X(1) VALUE 'N'.
01 MASTER-KEY-WORK PIC X(10).
01 TRANSACTION-KEY-WORK PIC X(10).
PROCEDURE DIVISION.
OPEN-FILES.
OPEN MASTER-FILE.
OPEN TRANSACTION-FILE.
PROCESS-TRANSACTION.
READ TRANSACTION-FILE
AT END MOVE 'Y' TO EOF-FLAG
NOT AT END PERFORM MATCH-MASTER.
CLOSE-FILES.
CLOSE MASTER-FILE.
CLOSE TRANSACTION-FILE.
STOP-RUN.
MATCH-MASTER.
MOVE TRANSACTION-KEY TO TRANSACTION-KEY-WORK.
SET MATCH-FOUND-FLAG TO 'N'.
SET ERROR-FLAG TO 'N'.
SET MASTER-FOUND-FLAG TO 'N'.
READ MASTER-FILE
INVALID KEY SET ERROR-FLAG TO 'Y'
NOT INVALID KEY
PERFORM CHECK-MATCH.
IF ERROR-FLAG = 'Y'
MOVE 'N' TO MATCH-FOUND-FLAG.
IF MATCH-FOUND-FLAG = 'N'
PERFORM PROCESS-NO-MATCH.
CHECK-MATCH.
MOVE MASTER-KEY TO MASTER-KEY-WORK.
IF TRANSACTION-KEY-WORK = MASTER-KEY-WORK
PERFORM PROCESS-MATCH
SET MATCH-FOUND-FLAG TO 'Y'.
IF MASTER-FOUND-FLAG = 'N'
SET MASTER-FOUND-FLAG TO 'Y'.
PROCESS-MATCH.
SET MATCH-FOUND-FLAG TO 'Y'.
SET MASTER-FOUND-FLAG TO 'Y'.
MOVE 'Y' TO TRANSACTION-FOUND-FLAG.
DISPLAY "Match found for key: " MASTER-KEY.
DISPLAY "Master data: " MASTER-DATA.
DISPLAY "Transaction data: " TRANSACTION-DATA.
PROCESS-NO-MATCH.
SET MATCH-FOUND-FLAG TO 'N'.
MOVE 'N' TO TRANSACTION-FOUND-FLAG.
DISPLAY "No match found for key: " TRANSACTION-KEY.
DISPLAY "Transaction data: " TRANSACTION-DATA.
```
In this program, the `MASTER-FILE` is an indexed file, and the `TRANSACTION-FILE` is a line-sequential file. The program reads each record from the `TRANSACTION-FILE`, and searches for a matching record in the `MASTER-FILE`. If a match is found, the program displays the master and transaction data. If no match is found, the program displays an error message.
阅读全文