1

I am trying to only print unique case numbers (which are located in the first 5 bytes of each input record). My input file structure is as in this example:

00001
00001
00002
00002
00002
00003
00004
00005

I should be able to read in the case number such as 0001 (or 00002, 00003, ...) and save that in a temp variable. Then compare the value of that temp variable to the case number of the current record being processed. When they do not match then I need to write the case number (as contained in the temp variable) to my output file. And then continue processing the remaining records.

So in the end, my output file should look similar to this one (note that only the last records with case number either 00001 or 00002 were written):

00001
00002
00003
00004
00005

Here is my code so far:

READ WORK FILE 1 #I-RECORD                            
 IF #RECORD-READ = 0 DO                                   
    WRITE WORK 2 #OUT-HEADER                              
 DOEND /* 510                                             
 ADD 1 TO #RECORD-READ                                    
*                                                         
IF #AA-CASE NOT EQUAL ' ' DO                              
 #CURRENT-CASE-NUM = #AA-CASE                             
 IF #COUNT-CASES = 0 DO                                   
    #OLD-CASE-NUM = #AA-CASE                              
 DOEND /* 570                                             
*                                                         
 IF #OLD-CASE-NUM NOT EQUAL #CURRENT-CASE-NUM DO          
    #OO-CASE-NUMBER            = #OLD-CASE-NUM            
*   #OO-TOTAL-OF-MONTHS        = 0                        
*   #OO-TOTAL-OF-TRANSACTIONS = #COUNT-CASES              
    WRITE WORK 2 #OUTPUT-RECORD                           
    #OLD-CASE-NUM = 0   
 DOEND  
*                                                      
 DISPLAY #CURRENT-CASE-NUM #OLD-CASE-NUM #COUNT-CASES  
*                                                      
 IF #OLD-CASE-NUM NOT EQUAL #CURRENT-CASE-NUM DO       
    #OO-CASE-NUMBER            = #OLD-CASE-NUM         
*   #OO-TOTAL-OF-MONTHS        = 0                     
*   #OO-TOTAL-OF-TRANSACTIONS = #COUNT-CASES           
    WRITE WORK 2 #OUTPUT-RECORD                        
    #OLD-CASE-NUM = 0                                  
 DOEND /* 610                                          
 ELSE DO                                               
   ADD 1 TO #COUNT-CASES                               
 DOEND /* 710                                          
DOEND /* 510                                                
LOOP(0500) 
4

1 回答 1

1

您的示例文件按顺序包含案例编号。这使问题变得更容易。您可以通过将当前案例编号与之前的案例编号进行比较来解决此问题(请参阅下面示例中的 TEST-CASE-NUMBER 段)。注意分配先前案例编号并将其与当前案例编号进行比较的顺序。下面的示例是为 GNUCobol 编写的,但您应该能够将其改编为大型机 Cobol。此外,在读取文件时,请始终检查文件状态以捕获打开、读取和关闭错误(TEST-FILE-STATUS 段)。

   IDENTIFICATION DIVISION.
   PROGRAM-ID. PRINT-UNIQUE.

   ENVIRONMENT DIVISION.
   INPUT-OUTPUT SECTION.
   FILE-CONTROL.
   SELECT CASE-FILE
       ASSIGN TO 'CASE-FILE.DAT'
       ORGANIZATION IS LINE SEQUENTIAL
       FILE STATUS IS CASE-FILE-STATUS.

   DATA DIVISION.
   FILE SECTION.
   FD CASE-FILE.
       01  CASE-NUMBER                  PIC X(5).

   WORKING-STORAGE SECTION.

   01  MORE-RECORDS                     PIC XXX VALUE 'YES'.
       88  HAS-MORE-RECORDS             VALUE 'YES'.
       88  NO-MORE-RECORDS              VALUE 'NO '.

   01  CASE-FILE-STATUS                 PIC XX.
       88  CASE-FILE-STATUS-OKAY        VALUES '00' '10'.
  *        VALUE 00 = SUCCESS
  *        VALUE 10 = END OF FILE

   01  PRIOR-CASE-NUMBER                PIC X(5) VALUE SPACES.

   PROCEDURE DIVISION.

   MAIN.
       OPEN INPUT CASE-FILE
       PERFORM TEST-FILE-STATUS
       PERFORM HANDLE-CASE-RECORD UNTIL NO-MORE-RECORDS
       CLOSE CASE-FILE
       PERFORM TEST-FILE-STATUS
       STOP RUN
       .

   HANDLE-CASE-RECORD.
       READ CASE-FILE
           AT END SET NO-MORE-RECORDS TO TRUE
           NOT AT END
               PERFORM TEST-CASE-NUMBER
       END-READ
       PERFORM TEST-FILE-STATUS
       .

   TEST-CASE-NUMBER.
       IF CASE-NUMBER NOT = PRIOR-CASE-NUMBER
           DISPLAY CASE-NUMBER
       END-IF
       MOVE CASE-NUMBER TO PRIOR-CASE-NUMBER
       .

   TEST-FILE-STATUS.
       IF NOT CASE-FILE-STATUS-OKAY THEN
           DISPLAY 'FILE READ ERROR ' CASE-FILE-STATUS
           CLOSE CASE-FILE
           STOP RUN
       END-IF
       .
于 2014-08-09T01:33:40.247 回答