1

让我们假设我有一个文件 A 具有以下数据:

A1.01  
A2.02  
A4.03   
A6.01

文件 B 具有以下数据:

A2.02 xyshahaslsl  
A2.02 dkjhsldhsds  
A2.02 ewewhrewjws  
A4.03 wejwejwrewl  
A4.03 wejedededee  
A5.01 kdkgskhdgss  
A5.02 fljsdfjdfjd  
A5.03 sdjdhsdhsld  
A7.04 jhsdhskdhsd  
A7.07 dsjdhslkhds  

如果它与第二个文件的记录的前两个字母数据匹配,则需要从文件B中删除文件A中存在的记录。

因此输出应如下所示:

A5.01 kdkgskhdgss  
A5.02 fljsdfjdfjd  
A5.03 sdjdhsdhsld  
A7.04 jhsdhskdhsd  
A7.07 dsjdhslkhds 

我的文件 A 有 400 条记录,文件 B 有 50 万条记录。我运行了一个 COBOL 程序,但运行时间过长。

cobol 程序逻辑如下:
这里我将包含 50 万条记录的文件作为 A。
将包含 400 条记录的文件作为文件 B。

WORKING-STORAGE SECTION.                         
01 WS-RECORDS-WRITTEN    PIC 9(8) VALUE ZEROES.  
01 WS-RECORDS-DELETED    PIC 9(8) VALUE ZEROES.  
01 WS-INSERT-FILE-STATUS PIC X(2).               
01 WS-EOFA PIC A(1) VALUE 'N'.                   
01 WS-EOFB PIC A(1) VALUE 'N'.                   
01 WS-FLAG-FOUND PIC A(1) VALUE 'N'. 



PROCEDURE DIVISION.                                    
MAIN-PARA.                                             
     OPEN INPUT IFILEA                                 
     OPEN OUTPUT OFILE                                 
     PERFORM PARA0 THRU PARA0-EXIT UNTIL WS-EOFA = "Y" 
     CLOSE IFILEA                                      
     CLOSE OFILE                                       
     STOP RUN.                                         
     EXIT.                                             
PARA0.                                                 
     READ IFILEA                                       
     AT END MOVE "Y" TO WS-EOFA                        
     END-READ                                          
     IF WS-EOFA = "N"                                  
        OPEN INPUT IFILEB                              
       MOVE "N" TO WS-EOFB                             
       MOVE "N" TO WS-FLAG-FOUND                       
         PERFORM PARA1 THRU PARA1-EXIT UNTIL WS-EOFB = "Y"  
             OR WS-FLAG-FOUND = "Y"                         
         IF WS-FLAG-FOUND = "N"                             
            WRITE F2RECORD FROM F0RECORD                    
         END-IF                                             
        CLOSE IFILEB                                        
      END-IF.                                               
 PARA0-EXIT.                                                
      EXIT.                                                 
 PARA1.                                                     
      READ IFILEB                                           
      AT END MOVE "Y" TO WS-EOFB                            
      END-READ                                              
      IF OSS024S-TRACKING-ID = OSS024V-REC                  
* SKIP THE RECORD                                           
        MOVE "Y" TO WS-FLAG-FOUND                           
      END-IF.                                               
 PARA1-EXIT.                                                
      EXIT.   
4

1 回答 1

3

[为清楚起见,添加了示例文件控制、文件部分和所需的工作存储。
跟随弹跳球查看其他涉及的文件。引用的文件名用于我的测试。只是为了简洁起见]

FILE-CONTROL.
    SELECT FILE-A ASSIGN TO "Q30309414.EXC"
            ORGANIZATION IS LINE SEQUENTIAL
        FILE STATUS  IS FILE-A-STATUS.
FILE SECTION.
FD  FILE-A.        
01  FILE-A-REC.
    05 FILE-A-FIELD    PIC X(02).
    05 FILLER          PIC X(18).
WORKING-STORAGE SECTION.
01  FILE-A-STATUS            PIC X(02).
    88 EOF-A                 VALUE '10'.
MAINLINE.
    OPEN INPUT FILE-A.
    OPEN INPUT FILE-B.
    OPEN OUTPUT FILE-O.
    MOVE LOW-VALUES          TO FILE-A-REC.
    PERFORM READ-AND-FILTER 
      UNTIL EOF-B.
    CLOSE FILE-A.
    CLOSE FILE-B.
    CLOSE FILE-O.
    STOP RUN.

READ-AND-FILTER.
    READ FILE-B AT END MOVE HIGH-VALUES TO FILE-B-REC.
    PERFORM READ-NEXT-FILTER
       UNTIL FILE-B-FIELD IS NOT GREATER THAN FILE-A-FIELD.
    IF FILE-B-FIELD IS LESS THAN FILE-A-FIELD
      WRITE FILE-O-REC FROM FILE-B-REC.

READ-NEXT-FILTER.
    IF FILE-B-FIELD IS GREATER THAN FILE-A-FIELD
      READ FILE-A
        AT END MOVE HIGH-VALUES TO FILE-A-REC.

不需要任何标志,只需要文件状态。当然不需要创建依赖于布局的代码的邪恶执行。

这假设您的数据已按照您的指示进行排序。如果不是,那么方法会有所不同 - 将有问题的排除文件字段(两个字符)读入工作存储表,对表进行排序和搜索,以查找与读取的每个主文件记录的匹配项。

于 2015-05-19T05:53:08.987 回答