2

我需要根据地区代码找出每个地区有多少政策。所以每次我在记录中找到区域时,我都需要在我的表中增加该区域的计数,然后我才能将它移动到我的输出中。根据下面的记录,我无法弄清楚如何计算每个地区的政策。我尝试了各种方法,但似乎没有任何效果。

如果您需要更多信息,请告诉我。

以下是记录的定义和摘录(共 57 条记录)

rec-94 型图片 x(2)
rec-94-policy-number pic x(8)
填充图片 x(5)
rec-94-parish-code pic x(3)
填充图片 x(1)
rec-94 区域代码 pic x(1)

94A 018517 080 1       
94A 027721 090 1      
94A 036470 250 6

94A 049137 010 1  
……

我最近的尝试:

05  T2-TERRITORY-COUNT.                                    
  10  FILLER                   PIC X(4) VALUE '1   '.    
  10  FILLER                   PIC X(4) VALUE '2   '.    
  10  FILLER                   PIC X(4) VALUE '3   '.    
  10  FILLER                   PIC X(4) VALUE '4   '.    
  10  FILLER                   PIC X(4) VALUE '5   '.    
  10  FILLER                   PIC X(4) VALUE '6   '.    
  10  FILLER                   PIC X(4) VALUE '7   '.    
  10  FILLER                   PIC X(4) VALUE '8   '.    
  10  FILLER                   PIC X(4) VALUE '9   '.    

05  T2-TERRITORY-TABLE REDEFINES T2-TERRITORY-COUNT.       
  10  T2-ENTRY                 OCCURS 9 TIMES            
                                 INDEXED BY T2-INDEX.    
      15 T2-TERRITORY-CODE       PIC X.                  
      15 T2-TERRITORY-COUNTER    PIC 999. 

A000-MAINLINE.                                     

 PERFORM B000-OPENING-PROCEDURE.                
 PERFORM B600-PRINT-HEADINGS.                   
 PERFORM B200-READ-FILE.                        
 PERFORM B300-MAIN-PROCEDURE                    
      UNTIL END-OF-FILE-SW = 'YES'.             
 PERFORM B800-MOVE-TERRITORY-CODE               
      VARYING T2-INDEX FROM 1 BY 1              
      UNTIL T2-INDEX > 9.                       

 PERFORM B110-MOVE-COUNTS                       
 PERFORM B100-CLOSING-PROCEDURE.                
 STOP RUN.                                      



   B000-OPENING-PROCEDURE.                                      
         OPEN OUTPUT REPORT-FILE.                               
         OPEN OUTPUT PRINT-FILE.                                
         OPEN INPUT INPUT-FILE.                                 

   B100-CLOSING-PROCEDURE.                                      
        PERFORM B500-PRINT-TOTAL-LINE.                         
        CLOSE REPORT-FILE.                                     
        CLOSE PRINT-FILE.                                      
        CLOSE INPUT-FILE.                                      

   B200-READ-FILE.                                              
        READ INPUT-FILE INTO RECORD-TYPE-94                    
            AT END MOVE 'YES' TO END-OF-FILE-SW.   

    B300-MAIN-PROCEDURE.                               
          IF REC-94-TYPE = "94"                        
                PERFORM B400-SEARCH-TERRITORY           
                PERFORM B900-COUNT-POLICIES             
           ELSE                                         
                 WRITE REPORT-RECORD FROM RECORD-TYPE-94   
           END-IF.                                      
           ADD 1 TO A-LINE-COUNT.                       
           PERFORM B200-READ-FILE.                      


     B400-SEARCH-TERRITORY.                                         
          SET T1-INDEX TO 1.                                        
          SEARCH T1-ENTRY                                           
               AT END                                               
                    DISPLAY 'PARISH NOT FOUND IN TABLE'             
                    CALL 'CEE3ABD' USING BY VALUE 12 BY VALUE 1     
               WHEN                                                 
                    REC-94-PARISH-CODE = T1-PARISH(T1-INDEX)        
                    MOVE T1-TERRITORY(T1-INDEX) TO  
                        REC-94-TERRITORY-CODE                      
                   ADD 1 TO A-DISK-COUNTER                         
                   PERFORM B700-MOVE-RECORDS                       
         END-SEARCH.                                               

    B500-PRINT-TOTAL-LINE.                                         
          MOVE A-LINE-COUNT TO TOTAL-RECORDS.                      
          MOVE A-DISK-COUNTER TO TOTAL-POLICIES.                   
          WRITE PRINT-RECORD FROM TOTAL-LINE.                      

    B600-PRINT-HEADINGS.                                           
          ADD 1 TO A-PAGE-COUNT.                                   
          MOVE A-PAGE-COUNT TO PRINT-PAGE-NUMBER.                  
          WRITE PRINT-RECORD FROM HEADER.                          
          WRITE PRINT-RECORD FROM HEADER-LINE-2.                   
          WRITE PRINT-RECORD FROM COLUMN-LINE.                     

    B700-MOVE-RECORDS.    
           MOVE REC-94-TYPE TO REC-94-TYPE-OUT                     
           MOVE REC-94-POLICY-NUMBER TO REC-94-POLICY-NUMBER-OUT   
           MOVE REC-94-PARISH-CODE TO REC-94-PARISH-CODE-OUT       
           MOVE REC-94-TERRITORY-CODE TO REC-94-TERRITORY-CODE-OUT 
           WRITE REPORT-RECORD FROM TRNREC94-OUT.                  

     B800-MOVE-TERRITORY-CODE.                                     
          MOVE T2-TERRITORY-CODE(T2-INDEX) TO DET-TERRITORY.       
          WRITE PRINT-RECORD FROM DETAIL-LINE.                     


     B900-COUNT-POLICIES.                                  
           MOVE ZEROES TO T2-TERRITORY-COUNTER(T2-INDEX).   
           SET T2-INDEX TO 1.                               
           SEARCH T2-ENTRY                                  
                 AT END                                       
                        DISPLAY 'NO POLICIES FOUND.'            
                 WHEN                                         
                        REC-94-TERRITORY-CODE =                 
                              T2-TERRITORY-CODE(T2-INDEX)        
                        ADD 1 TO T2-TERRITORY-COUNTER(T2-INDEX) 
            END-SEARCH.

          MOVE T2-TERRITORY-COUNTER(T2-INDEX) TO DET-NUMBER-POLICIES.
          WRITE PRINT-RECORD FROM DETAIL-LINE. 

我将不胜感激任何指示或正确的方向。在此先感谢!

我的最终代码:

        PERFORM B000-OPENING-PROCEDURE.                              
        PERFORM B600-PRINT-HEADINGS.                                 
        PERFORM B200-READ-FILE.                                      
        PERFORM B300-MAIN-PROCEDURE                                  
             UNTIL END-OF-FILE-SW = 'YES'.                           
        PERFORM C100-MOVE-COUNTS                                     
             VARYING T2-INDEX FROM 1 BY 1                            
             UNTIL T2-INDEX > 9.                                     
        PERFORM B100-CLOSING-PROCEDURE.                              
        STOP RUN.                                                    

    B000-OPENING-PROCEDURE.                                          
          OPEN OUTPUT REPORT-FILE.                                   
          OPEN OUTPUT PRINT-FILE.                                    
          OPEN INPUT INPUT-FILE.                                     

    B100-CLOSING-PROCEDURE.                                          
          PERFORM B500-PRINT-TOTAL-LINE.  
          CLOSE REPORT-FILE.                                         
          CLOSE PRINT-FILE.                                          
          CLOSE INPUT-FILE.                                          

    B200-READ-FILE.                                                  
          READ INPUT-FILE INTO RECORD-TYPE-94                        
               AT END MOVE 'YES' TO END-OF-FILE-SW.                  

    B300-MAIN-PROCEDURE.                                             
          IF REC-94-TYPE = "94"                                      
               PERFORM B400-SEARCH-TERRITORY                         
          ELSE                                                       
             WRITE REPORT-RECORD FROM RECORD-TYPE-94                 
          END-IF.                                                    
          ADD 1 TO A-LINE-COUNT.                                     
          PERFORM B200-READ-FILE. 
     B400-SEARCH-TERRITORY.                                          
          SET T1-INDEX TO 1.                                         
          SEARCH T1-ENTRY                                            
               AT END                                                
                    DISPLAY 'PARISH NOT FOUND IN TABLE'              
                    CALL 'CEE3ABD' USING BY VALUE 12 BY VALUE 1      
               WHEN                                                  
                    REC-94-PARISH-CODE = T1-PARISH(T1-INDEX)         
                    MOVE T1-TERRITORY(T1-INDEX) TO                   
                         REC-94-TERRITORY-CODE                       
                    ADD 1 TO A-DISK-COUNTER                          
                    PERFORM B700-MOVE-RECORDS                        
                    PERFORM B900-COUNT-POLICIES                      
          END-SEARCH.                                                

     B500-PRINT-TOTAL-LINE.                                          
           MOVE A-LINE-COUNT TO TOTAL-RECORDS.                       
           MOVE A-DISK-COUNTER TO TOTAL-POLICIES.       
           WRITE PRINT-RECORD FROM TOTAL-LINE.                      

     B600-PRINT-HEADINGS.                                           
           ADD 1 TO A-PAGE-COUNT.                                   
           MOVE A-PAGE-COUNT TO PRINT-PAGE-NUMBER.                  
           WRITE PRINT-RECORD FROM HEADER.                          
           WRITE PRINT-RECORD FROM HEADER-LINE-2.                   
           WRITE PRINT-RECORD FROM COLUMN-LINE.                     

     B700-MOVE-RECORDS.                                             
           MOVE REC-94-TYPE TO REC-94-TYPE-OUT                      
           MOVE REC-94-POLICY-NUMBER TO REC-94-POLICY-NUMBER-OUT    
           MOVE REC-94-PARISH-CODE TO REC-94-PARISH-CODE-OUT        
           MOVE REC-94-TERRITORY-CODE TO REC-94-TERRITORY-CODE-OUT  
           WRITE REPORT-RECORD FROM TRNREC94-OUT.                   

     B900-COUNT-POLICIES.                                           
           SET T2-INDEX TO 1.                                         
           SEARCH T2-ENTRY                                            
           AT END                                                 
               DISPLAY 'NO POLICIES FOUND.'                       
           WHEN                                                   
               REC-94-TERRITORY-CODE = T2-TERRITORY-CODE(T2-INDEX)
               DD 1 TO T2-TERRITORY-COUNTER(T2-INDEX)            
           END-SEARCH.                                                   


     C100-MOVE-COUNTS.                                               
          MOVE T2-TERRITORY-CODE(T2-INDEX) TO DET-TERRITORY.         
          MOVE T2-TERRITORY-COUNTER(T2-INDEX) TO DET-NUMBER-POLICIES.
          WRITE PRINT-RECORD FROM DETAIL-LINE.      
4

2 回答 2

5

带有搜索语句的 B900 版本应该可以工作,但是从 A000 调用 B900,并且只调用一次。将 PERFORM B900 语句移动到 B300,您应该为读取的每条记录收集一个计数。

此外,T2-TERRITORY-COUNTER 用空格初始化。请用零初始化它。根据您的编译器,它可能没有什么不同,但如果它从零开始,则更容易理解变量的意图。

* 更新 * 您更新后的代码仍有 T2-TERRITORY-COUNTER 的空格。也许以下内容会有所帮助。它基于您的代码,但删除了一些部分以使相关部分更易于查看。下面的代码适用于 GNU Cobol(以前的 OpenCobol - 参见 sourceforge.net)。

   IDENTIFICATION DIVISION.
   PROGRAM-ID. COUNT-TERRITORY.

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

   SELECT REPORT-FILE ASSIGN TO 'POLICY.RPT'
       FILE STATUS IS REPORT-FILE-STATUS
       ORGANIZATION IS LINE SEQUENTIAL.

   DATA DIVISION.
   FILE SECTION.
   FD  POLICY-FILE.
   01  POLICY-RECORD               PIC X(20).

   FD  REPORT-FILE.
   01  REPORT-RECORD               PIC X(132).

   WORKING-STORAGE SECTION.

   01  IS-POLICY-FILE-AT-END       PIC XXX VALUE 'NO '.
       88  POLICY-FILE-AT-END      VALUE 'YES'.

   01  POLICY-FILE-STATUS          PIC 9(2).
       88  POLICY-FILE-OK          VALUES 0 10.
  *                                VALUE 00 = SUCCESS 
  *                                VALUE 10 = END OF FILE

   01  REPORT-FILE-STATUS          PIC 9(2).
       88  REPORT-FILE-OK          VALUES 0 10.
  *                                VALUE 00 = SUCCESS 
  *                                VALUE 10 = END OF FILE

   01  RECORD-TYPE-94.
       05  REC-94-TYPE             PIC X(2).
           88  INCLUDED-RECORD-TYPE    VALUE '94'.
       05  REC-94-POLICY-NUMBER    PIC X(8).
       05  FILLER                  PIC X(5).
       05  REC-94-PARISH-CODE      PIC X(3).
       05  FILLER                  PIC X(1).
       05  REC-94-TERRITORY-CODE   PIC X(1).

   01  T2-TERRITORY-COUNT.                                    
       05  FILLER                  PIC X(4) VALUE '1000'.    
       05  FILLER                  PIC X(4) VALUE '2000'.    
       05  FILLER                  PIC X(4) VALUE '3000'.    
       05  FILLER                  PIC X(4) VALUE '4000'.    
       05  FILLER                  PIC X(4) VALUE '5000'.    
       05  FILLER                  PIC X(4) VALUE '6000'.    
       05  FILLER                  PIC X(4) VALUE '7000'.    
       05  FILLER                  PIC X(4) VALUE '8000'.    
       05  FILLER                  PIC X(4) VALUE '9000'.    

   01  T2-TERRITORY-TABLE REDEFINES T2-TERRITORY-COUNT.       
       05  T2-ENTRY                OCCURS 9 TIMES            
           INDEXED BY T2-INDEX.   
           10 T2-TERRITORY-CODE    PIC X.                  
           10 T2-TERRITORY-COUNTER PIC 999. 

   PROCEDURE DIVISION.

   A000-MAINLINE.                                     
       PERFORM B000-OPENING-PROCEDURE
       PERFORM B200-READ-FILE
       PERFORM B300-MAIN-PROCEDURE
           UNTIL POLICY-FILE-AT-END
       PERFORM C100-WRITE-TERRITORY-COUNTS
       PERFORM B100-CLOSING-PROCEDURE
       STOP RUN
       .

   B000-OPENING-PROCEDURE.                                      
       OPEN INPUT  POLICY-FILE
       OPEN OUTPUT REPORT-FILE
       .

   B100-CLOSING-PROCEDURE.                                      
       CLOSE POLICY-FILE
       CLOSE REPORT-FILE
       .

   B200-READ-FILE.                                              
       READ POLICY-FILE INTO RECORD-TYPE-94                    
           AT END SET POLICY-FILE-AT-END TO TRUE
       PERFORM D100-CHECK-POLICY-FILE-STATUS
       .

   B300-MAIN-PROCEDURE.                               
       IF INCLUDED-RECORD-TYPE
           PERFORM B900-COUNT-POLICIES
       ELSE
           WRITE REPORT-RECORD FROM RECORD-TYPE-94
           PERFORM D200-CHECK-REPORT-FILE-STATUS
       END-IF
       PERFORM B200-READ-FILE
       .

   B900-COUNT-POLICIES.
       SET T2-INDEX TO 1
       SEARCH T2-ENTRY
           AT END
               DISPLAY 'TERRITORY ' REC-94-TERRITORY-CODE
                       ' UNKNOWN'
           WHEN REC-94-TERRITORY-CODE = T2-TERRITORY-CODE (T2-INDEX)
               ADD 1 TO T2-TERRITORY-COUNTER (T2-INDEX)
       END-SEARCH
       .

   C100-WRITE-TERRITORY-COUNTS.
       MOVE SPACES TO REPORT-RECORD
       WRITE REPORT-RECORD
       PERFORM D200-CHECK-REPORT-FILE-STATUS
       PERFORM VARYING T2-INDEX FROM 1 BY 1
           UNTIL T2-INDEX > 9
           STRING 'POLICY COUNT FOR TERRITORY '
                  T2-TERRITORY-CODE (T2-INDEX)
                  ': '
                  T2-TERRITORY-COUNTER (T2-INDEX)
           INTO REPORT-RECORD
           WRITE REPORT-RECORD
           PERFORM D200-CHECK-REPORT-FILE-STATUS
       END-PERFORM
       .

   D100-CHECK-POLICY-FILE-STATUS.
       IF NOT POLICY-FILE-OK
           DISPLAY 'ERROR CODE READING POLICY FILE: '
                   POLICY-FILE-STATUS
       END-IF
       .

   D200-CHECK-REPORT-FILE-STATUS.
       IF NOT REPORT-FILE-OK
           DISPLAY 'ERROR CODE WRITING REPORT FILE: '
                   POLICY-FILE-STATUS
       END-IF
       .

于 2013-11-01T17:28:36.797 回答
1

在已经到达文件结尾之后,您只执行一次 B900-COUNT-POLICIES。

B900- 也只是使用 T2-INDEX 上次拥有的任何值。

您有两个主要选择:要么循环执行;要么 或者,假设您的区域是零到小于或等于九 - 在这种情况下,您可以使用区域的值来设置索引值并添加。搜索是可能的(它对你不起作用的原因仍然是仅在文件结束后执行段落,而不是每条记录),但根据我的经验,它不是为这种类型选择的方法任务。

如果要使用区域来获取用于 ADD 的索引值,请使用 SET:

SET T2-INDEX TO rec-94-territory-code

除非你不能。rec-94-territory-code 是一个字母数字(一个 PIC X 字段。这适用于计算中未使用的任何内容)。在您的 WORKING-STORAGE 中定义一个新的数字字段并首先

MOVE rec-94-territory-code TO new-numeric-field

然后

SET T2-INDEX TO new-numeric-field

对于循环,我认为您已经可以到达那里。

但是,在您以任何一种方式进行任何添加之前,如果您的每个区域计数从零开始,这将是一个好主意。你在太空的状态。即使这“有效”,也不是好的做法。

所以你需要从零开始——现在循环很好。

根据您的代码更改,您的下一个问题是您如何尝试为计数表设置初始值。

现在,在您的 OPEN 段落之后添加一个段落并执行该新段落。在该段落中,进行循环以将表中的值设置为零,从第一个开始到第九个结束。

于 2013-11-01T17:21:00.423 回答