如何使用if语句跳过COBOL中的记录

时间:2013-10-29 23:19:43

标签: cobol

              FD  STUDENTS-FILE-IN.
   01  STUDENTS-RECORD-IN.
       05  SSN-IN                  PIC X(9).
       05  STUDENT-NAME-IN         PIC X(11).
       05                          PIC X(5).
       05  GRAD-STATUS-IN          PIC X.
       05  CLASS-STANDING-IN       PIC X.
       05  MAJOR-IN                PIC X(3).
       05  CREDIT-HOURS-IN         PIC 9(3).
       05  CREDIT-POINTS-IN        PIC 9(3).

   FD  STUDENTS-FILE-OUT.
   01  STUDENTS-RECORD-OUT         PIC X(80).

   WORKING-STORAGE SECTION.
   01  ARE-THERE-MORE-RECORDS      PIC X(3)  VALUE 'YES'.
   01  PAGE-NUMBER                 PIC 99    VALUE ZERO.
   01  LINE-COUNT                  PIC 99    VALUE ZERO.
   01  SSID-BREAK.
       03 FIRST-PART               PIC X(3).
       03 SECOND-PART              PIC X(2).
       03 THIRD-PART               PIC X(4).
   01  NAME-BREAK.
       03 FIRST-LETTER             PIC X(1).
       03 MIDDLE-LETTER            PIC X(1).
       03 LAST-LETTER              PIC X(10).
   01  GRAD-CHECK                  PIC X.
   01  WS-DATE.
       05 RUN-YEAR                 PIC XX.
       05 RUN-MONTH                PIC XX.
       05 RUN-DAY                  PIC XX.

   01  HEADING-LINE-ONE.
       05                          PIC X(21) VALUE SPACES.
       05                          PIC X(33)
               VALUE 'RHODES STATE COLLEGE GRADE REPORT'.
       05                          PIC X(6) VALUE SPACES.
       05      HEADING-LINE-DATE.
               10  MONTH-NOW       PIC XX.
               10                  PIC X     VALUE '/'.
               10  DAY-NOW         PIC XX.
               10                  PIC X     VALUE '/'.
               10  YEAR-NOW        PIC XX.

       05                          PIC X(3)  VALUE SPACES.
       05                          PIC X(4)  VALUE 'PAGE'.
       05  HL-1-PAGE-NUMBER        PIC Z9    VALUE ZEROS.

   01  HEADING-LINE-TWO.
       05                          PIC X(10) VALUE 'SOC SEC NO'.
       05                          PIC X(4)  VALUE SPACES.
       05                          PIC X(12) VALUE 'STUDENT NAME'.
       05                          PIC X(3)  VALUE SPACES.
       05                          PIC X(8)  VALUE 'STANDING'.
       05                          PIC X(4)  VALUE SPACES.
       05                          PIC X(5)  VALUE 'MAJOR'.
       05                          PIC X(10) VALUE SPACES.
       05                          PIC X(5)  VALUE 'HOURS'.
       05                          PIC X(2)  VALUE SPACES.
       05                          PIC X(6)  VALUE 'POINTS'.
       05                          PIC X(5)  VALUE SPACES.
       05                          PIC X(3)  VALUE 'GPA'.

   01  DETAIL-LINE.

       05  DL-SSID.
           10 SSID-1               PIC X(3).
           10                      PIC X     VALUE "-".
           10 SSID-2               PIC X(2).
           10                      PIC X     VALUE "-".
           10 SSID-3               PIC X(4).
       05  BLANK-B                 PIC X(3)  VALUE SPACES.
       05  DL-NAME .
           10 FIRST-INI            PIC X.
           10                      PIC X     VALUE SPACES.
           10 MID-INI              PIC X.
           10                      PIC X     VALUE SPACES.
           10 LAST-NAME            PIC X(10).

       05  BLANK-C                 PIC X(3)  VALUE SPACES.
       05  YEAR-STATUS             PIC X(9).
       05  BLANK-D                 PIC X(3)  VALUE SPACES.
       05  STUDENT-MAJOR           PIC X(13).
       05  BLANK-E                 PIC X(5)  VALUE SPACES.
       05  STUDNET-HOURS           PIC ZZ9.
       05  BLANK-F                 PIC X(5)  VALUE SPACES.
       05  STUDENT-POINTS          PIC ZZ9.
       05  BLANK-G                 PIC X(4)  VALUE SPACES.
       05  STUDENT-GPA             PIC 9V99.

   01  TOTALS-LINE.

       05 TOTALS                   PIC X(6)  VALUE 'TOTALS'.
       05                          PIC X(34) VALUE  SPACES.
       05 HITS-TOTAL               PIC ZZZ,ZZZ.
       05                          PIC X(9) VALUE SPACES.
       05 BATS-TOTAL               PIC ZZZ,ZZZ.
       05                          PIC X(10) VALUE  SPACES.
       05 AVG-TOTAL                PIC .999.

   PROCEDURE DIVISION.
   100-MAIN.
       OPEN INPUT STUDENTS-FILE-IN
       OPEN OUTPUT STUDENTS-FILE-OUT

       ACCEPT WS-DATE FROM DATE
       MOVE RUN-MONTH TO MONTH-NOW
       MOVE RUN-DAY TO DAY-NOW
       MOVE RUN-YEAR TO YEAR-NOW

       PERFORM 300-WRITE-HEADINGS

       PERFORM UNTIL ARE-THERE-MORE-RECORDS = 'NO '
           READ STUDENTS-FILE-IN
               AT END
                    MOVE 'NO ' TO ARE-THERE-MORE-RECORDS
                NOT AT END
                    PERFORM 200-PROCESS-ONE-RECORD
            END-READ
        END-PERFORM

       CLOSE STUDENTS-FILE-IN
       CLOSE STUDENTS-FILE-OUT
       STOP RUN.

   200-PROCESS-ONE-RECORD.

       IF  LINE-COUNT >= 53
          PERFORM 300-WRITE-HEADINGS
       END-IF


  *    IF  GRAD-STATUS-IN NOT = '1' AND NOT = '2'
  *        PERFORM 400-WRITE-TOTALS.
  *    END-IF

       MOVE SSN-IN TO SSID-BREAK
       MOVE FIRST-PART TO SSID-1
       MOVE SECOND-PART TO SSID-2
       MOVE THIRD-PART TO SSID-3
       MOVE STUDENT-NAME-IN TO NAME-BREAK
       MOVE FIRST-LETTER TO FIRST-INI
       MOVE MIDDLE-LETTER TO MID-INI
       MOVE LAST-LETTER TO LAST-NAME
       MOVE GRAD-STATUS-IN TO GRAD-CHECK
       IF  GRAD-CHECK = 'Y'

       END-IF



       MOVE DETAIL-LINE TO STUDENTS-RECORD-OUT

       WRITE STUDENTS-RECORD-OUT
           AFTER ADVANCING 1 LINES
       ADD 1 TO LINE-COUNT.

输入文件如下所示

307662099KRAlexander     Y2NES005017    

基本上某些文件不符合要求,我们只是想跳过它们。这是我们不想写入输出文件并跳过的文件示例。如果我解释得很糟糕,我很抱歉,但我真的很挣COBOL。

1 个答案:

答案 0 :(得分:1)

你的200段需要做这样的事情:

IF NOT ( <condition-for-skipping )
    PERFORM PROCESS-THIS-RECORD
ELSE 
    PERFORM IGNORE-THIS-RECORD
END-IF

您可以轻松交换条件

IF ( <condition-for-skipping )
    PERFORM IGNORE-THIS-RECORD
ELSE 
    PERFORM PROCESS-THIS-RECORD
END-IF

目前你在200段中拥有的所有东西,你都加入了一个新的paragrpah PROCESS-THIS-RECORD。你应该有一个新的段落IGNORE-THIS-RECORD。如果没有别的,它可以计算被忽略的记录。然后,如果计算已处理的记录并计算输入记录,最后可以检查是否已处理或忽略所有内容。

您应该检查文件状态。最好使用范围分隔符(如END-IF)并将完全停止/句点保持在最低限度。

88s很适合用于条件。保存大量文字,使维护更加复杂。