如何只打印唯一的案例编号?

时间:2014-08-08 18:28:23

标签: mainframe adabas-natural

我试图只打印唯一的案例编号(位于每个输入记录的前5个字节中)。我的输入文件结构如下例所示:

00001
00001
00002
00002
00002
00003
00004
00005

我应该能够读取案例编号,例如0001(或00002,00003,...),并将其保存在临时变量中。然后将该临时变量的值与正在处理的当前记录的案例编号进行比较。当它们不匹配时,我需要将案例编号(包含在temp变量中)写入我的输出文件。然后继续处理剩余的记录。

所以最后,我的输出文件应该与此类似(请注意,只写了案例编号为00001或00002的最后一条记录):

00001
00002
00003
00004
00005

到目前为止,这是我的代码:

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) 

1 个答案:

答案 0 :(得分: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
       .