COBOL 中的“NOT ON OVERFLOW”有什么用?
What's the use of `NOT ON OVERFLOW` in COBOL?
根据我在使用 UNSTRING
时的理解,如果在使用 UNSTRING
时会出现溢出,那么使用 ON OVERFLOW [INSTRUCTION]
将很有用。
但是如果没有溢出,为什么要使用 NOT ON OVERFLOW [INSTRUCTION]
呢?
NOT ON OVERFLOW [INSTRUCTION]
唯一可能的用途是在没有溢出的情况下传递一条指令,但如果您已经从 UNSTRING
获得预期结果,那么它有什么用呢?
有没有具体的例子说明这在这个例子中是如何有用的:
IDENTIFICATION DIVISION.
PROGRAM-ID. YOUR-PROGRAM-NAME.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WS-VAR1 PIC A(11) VALUE "Hello World".
01 WS-VAR2 PIC A(5).
01 WS-VAR3 PIC A(5).
01 WS-COMPTEUR PIC 9 VALUE 2.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
INTO WS-VAR2 WS-VAR3
WITH POINTER WS-COMPTEUR
ON OVERFLOW DISPLAY "This string is too large"
END-UNSTRING.
DISPLAY WS-VAR2
DISPLAY WS-VAR3.
STOP RUN.
END PROGRAM YOUR-PROGRAM-NAME.
即使在我阅读 IBM 文档时,它也没有给我太多关于在这种情况下可以使用什么的解释,而是显示一条消息?
IBM 文档:link
如果您需要调用 PROCEDURE 来验证这句话,例如 SORT-COLORS,您可以使用 "NOT ON OVERFLOW " 语句。
MOVE 0 TO COUNT-1.
UNSTRING COLOR-LIST
DELIMITED BY ":" OR "/" OR ALL SPACE
*DELIMIT-1 and COUNT-1 will hold only
*the values associated with COLOR-1.
INTO COLOR-1
DELIMITER IN DELIMIT-1
COUNT IN COUNT-1,
COLOR-2,
COLOR-3,
COLOR-4
ON OVERFLOW
DISPLAY "overflow: unstring colors"
NOT ON OVERFLOW
*do when UNSTRING succeeds.
PERFORM **SORT-COLORS**
END-UNSTRING.
*COLOR-1 = "RED "
*COLOR-2 = "BLUE "
*COLOR-3 = "GREEN "
*COLOR-4 = "YELLOW"
*DELIMIT-1 = ": "
*COUNT-1 = 3 count-1 holds the number of characters in RED
你可以看到更多关于这个的例子linkhttps://supportline.microfocus.com/documentation/acucorpproducts/docs/v6_online_doc/gtman3/gt36141.htm
在实践中,如果您需要发现您的命令 (unsting) 是否在没有 IF 的情况下成功执行,您可以使用此语句来定义它。
使用您的应用程序的示例
IDENTIFICATION DIVISION.
PROGRAM-ID. YOUR-PROGRAM-NAME.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WS-VAR1 PIC A(11) VALUE "Hello World".
01 WS-VAR2 PIC A(5).
01 WS-VAR3 PIC A(5).
01 WS-COMPTEUR PIC 9 VALUE 2.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
INTO WS-VAR2 WS-VAR3
WITH POINTER WS-COMPTEUR
ON OVERFLOW
PERFORM RT-SEND-ERROR-MAIL
NOT ON OVERFLOW
PERFORM RT-SEND-SUCESS-MAIL
END-UNSTRING.
DISPLAY WS-VAR2
DISPLAY WS-VAR3.
STOP RUN.
END PROGRAM YOUR-PROGRAM-NAME.
From my understanding when using UNSTRING, the use of ON OVERFLOW
phrase will be useful if there would be an overflow in the use
of the UNSTRING.
But if there is no overflow, why would you use NOT ON OVERFLOW
phrase ?
The only possible utility to the NOT ON OVERFLOW phrase would
be to pass an instruction if there is no overflow but what would be
the use of that if you already had the expected result from the
UNSTRING ?
在 COBOL 74 中没有 NOT ON OVERFLOW
短语。因此,有必要使用 GO TO
语句或在紧随其后的 IF
语句中设置测试标志。在 COBOL 85 中添加了 NOT ON OVERFLOW
短语和 END-UNSTRING
以改进程序结构。
对于您的示例,我做了一些更改,首先显示 WS-VAR1
,然后显示 UNSTRING
的结果。请注意,OVERFLOW
条件涉及项目的数量而不是文本的长度。我运行三个测试来展示结果
IDENTIFICATION DIVISION.
PROGRAM-ID. YOUR-PROGRAM-NAME.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-VAR1 PIC A(11) VALUE "Hello World".
01 WS-VAR2 PIC A(5).
01 WS-VAR3 PIC A(5).
PROCEDURE DIVISION.
MAIN-PROCEDURE.
MOVE SPACE TO WS-VAR2 WS-VAR3
DISPLAY WS-VAR1
UNSTRING WS-VAR1
DELIMITED SPACE
INTO WS-VAR2 WS-VAR3
ON OVERFLOW
DISPLAY
"Incorrect number of items in WS-VAR1 - expected 2"
NOT ON OVERFLOW
DISPLAY WS-VAR2
DISPLAY WS-VAR3
END-UNSTRING
STOP RUN.
END PROGRAM YOUR-PROGRAM-NAME.
结果:
Hello World
Hello
World
--
Hello
Incorrect number of items in WS-VAR1 - expected 2
--
Hello W rld
Incorrect number of items in WS-VAR1 - expected 2
代码中可以看出,ON OVERFLOW
用于错误处理; NOT ON OVERFLOW
用于正常处理。如果没有 NOT ON OVERFLOW
的改进结构,正常处理将紧跟在错误处理之后,除非使用前面提到的 COBOL 74 类型处理。
请注意,DELIMITED ALL SPACE
给出的结果与上面一种情况下显示的结果不同。 [忽略--
]
--
Hello
Hello
--
根据我在使用 UNSTRING
时的理解,如果在使用 UNSTRING
时会出现溢出,那么使用 ON OVERFLOW [INSTRUCTION]
将很有用。
但是如果没有溢出,为什么要使用 NOT ON OVERFLOW [INSTRUCTION]
呢?
NOT ON OVERFLOW [INSTRUCTION]
唯一可能的用途是在没有溢出的情况下传递一条指令,但如果您已经从 UNSTRING
获得预期结果,那么它有什么用呢?
有没有具体的例子说明这在这个例子中是如何有用的:
IDENTIFICATION DIVISION.
PROGRAM-ID. YOUR-PROGRAM-NAME.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WS-VAR1 PIC A(11) VALUE "Hello World".
01 WS-VAR2 PIC A(5).
01 WS-VAR3 PIC A(5).
01 WS-COMPTEUR PIC 9 VALUE 2.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
INTO WS-VAR2 WS-VAR3
WITH POINTER WS-COMPTEUR
ON OVERFLOW DISPLAY "This string is too large"
END-UNSTRING.
DISPLAY WS-VAR2
DISPLAY WS-VAR3.
STOP RUN.
END PROGRAM YOUR-PROGRAM-NAME.
即使在我阅读 IBM 文档时,它也没有给我太多关于在这种情况下可以使用什么的解释,而是显示一条消息?
IBM 文档:link
如果您需要调用 PROCEDURE 来验证这句话,例如 SORT-COLORS,您可以使用 "NOT ON OVERFLOW " 语句。
MOVE 0 TO COUNT-1.
UNSTRING COLOR-LIST
DELIMITED BY ":" OR "/" OR ALL SPACE
*DELIMIT-1 and COUNT-1 will hold only
*the values associated with COLOR-1.
INTO COLOR-1
DELIMITER IN DELIMIT-1
COUNT IN COUNT-1,
COLOR-2,
COLOR-3,
COLOR-4
ON OVERFLOW
DISPLAY "overflow: unstring colors"
NOT ON OVERFLOW
*do when UNSTRING succeeds.
PERFORM **SORT-COLORS**
END-UNSTRING.
*COLOR-1 = "RED "
*COLOR-2 = "BLUE "
*COLOR-3 = "GREEN "
*COLOR-4 = "YELLOW"
*DELIMIT-1 = ": "
*COUNT-1 = 3 count-1 holds the number of characters in RED
你可以看到更多关于这个的例子linkhttps://supportline.microfocus.com/documentation/acucorpproducts/docs/v6_online_doc/gtman3/gt36141.htm
在实践中,如果您需要发现您的命令 (unsting) 是否在没有 IF 的情况下成功执行,您可以使用此语句来定义它。
使用您的应用程序的示例
IDENTIFICATION DIVISION.
PROGRAM-ID. YOUR-PROGRAM-NAME.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WS-VAR1 PIC A(11) VALUE "Hello World".
01 WS-VAR2 PIC A(5).
01 WS-VAR3 PIC A(5).
01 WS-COMPTEUR PIC 9 VALUE 2.
PROCEDURE DIVISION.
MAIN-PROCEDURE.
INTO WS-VAR2 WS-VAR3
WITH POINTER WS-COMPTEUR
ON OVERFLOW
PERFORM RT-SEND-ERROR-MAIL
NOT ON OVERFLOW
PERFORM RT-SEND-SUCESS-MAIL
END-UNSTRING.
DISPLAY WS-VAR2
DISPLAY WS-VAR3.
STOP RUN.
END PROGRAM YOUR-PROGRAM-NAME.
From my understanding when using UNSTRING, the use of ON OVERFLOW phrase will be useful if there would be an overflow in the use of the UNSTRING.
But if there is no overflow, why would you use NOT ON OVERFLOW phrase ?
The only possible utility to the NOT ON OVERFLOW phrase would be to pass an instruction if there is no overflow but what would be the use of that if you already had the expected result from the UNSTRING ?
在 COBOL 74 中没有 NOT ON OVERFLOW
短语。因此,有必要使用 GO TO
语句或在紧随其后的 IF
语句中设置测试标志。在 COBOL 85 中添加了 NOT ON OVERFLOW
短语和 END-UNSTRING
以改进程序结构。
对于您的示例,我做了一些更改,首先显示 WS-VAR1
,然后显示 UNSTRING
的结果。请注意,OVERFLOW
条件涉及项目的数量而不是文本的长度。我运行三个测试来展示结果
IDENTIFICATION DIVISION.
PROGRAM-ID. YOUR-PROGRAM-NAME.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-VAR1 PIC A(11) VALUE "Hello World".
01 WS-VAR2 PIC A(5).
01 WS-VAR3 PIC A(5).
PROCEDURE DIVISION.
MAIN-PROCEDURE.
MOVE SPACE TO WS-VAR2 WS-VAR3
DISPLAY WS-VAR1
UNSTRING WS-VAR1
DELIMITED SPACE
INTO WS-VAR2 WS-VAR3
ON OVERFLOW
DISPLAY
"Incorrect number of items in WS-VAR1 - expected 2"
NOT ON OVERFLOW
DISPLAY WS-VAR2
DISPLAY WS-VAR3
END-UNSTRING
STOP RUN.
END PROGRAM YOUR-PROGRAM-NAME.
结果:
Hello World
Hello
World
--
Hello
Incorrect number of items in WS-VAR1 - expected 2
--
Hello W rld
Incorrect number of items in WS-VAR1 - expected 2
代码中可以看出,ON OVERFLOW
用于错误处理; NOT ON OVERFLOW
用于正常处理。如果没有 NOT ON OVERFLOW
的改进结构,正常处理将紧跟在错误处理之后,除非使用前面提到的 COBOL 74 类型处理。
请注意,DELIMITED ALL SPACE
给出的结果与上面一种情况下显示的结果不同。 [忽略--
]
--
Hello
Hello
--