ADD.Handling text strings.
SUBTRACT.
MULTIPLY.
DIVIDE.
COMPUTE.
Intrinsic Functions
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
one where the receiving variable is part of the calculation and
another where the original value in the receiving variable is discarded.
[This chapter] [TOC] [Tutorial]
ADD {identifier-1 | literal-1} ... TO {identifier-2 [ROUNDED]} ...
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-ADD]
All the "{, ( , ) , }, [, ]" state what is legal uses of the ADD. ADD figure, 2 TO teller.In pascal this statement will look like this:
teller := teller + figure + 2
[This chapter] [TOC] [Tutorial]
ADD {identifier-1 | literal-1} ... TO {identifier-2 | literal-2}
GIVING {identifier-3 [ROUNDED]}
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-ADD]
ADD figure TO 2 GIVING teller.In pascal this statement will look like this:
teller := figure + 2
[This chapter] [TOC] [Tutorial]
SUBTRACT {identifier-1 | literal-1} ... FROM {identifier-2 [ROUNDED]} ...
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-SUBTRACT]
SUBTRACT figure, 2 FROM teller.In pascal this statement will look like this:
teller := teller - figure - 2
SUBTRACT {identifier-1 | literal-1} ... FROM {identifier-2 | literal-2}
GIVING {identifier-3 [ROUNDED]}
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-ADD]
SUBTRACT figure FROM 2 GIVING teller.In pascal this statement will look like this:
teller := 2 - figure
[This chapter] [TOC] [Tutorial]
MULTIPLY {identifier-1 | literal-1} BY {identifier-2 [ROUNDED]} ...
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-MULTIPLY]
MULTIPLY teller BY 2, figure.In pascal this statement will look like this:
teller := teller * figure * 2
MULTIPLY {identifier-1 | literal-1} BY {identifier-2 | literal-2}
GIVING {identifier-3 [ROUNDED]}
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-MULTIPLY]
MULTIPLY figure BY 2 GIVING teller.In pascal this statement will look like this:
teller := figure * 2
[This chapter] [TOC] [Tutorial]
DIVIDE 2 INTO teller.In pascal this statement will look like this:
teller := teller / 2
DIVIDE {identifier-1 | literal-1} [ INTO | BY ] {identifier-2 | literal-2}
GIVING {identifier-3 [ROUNDED]}
[REMAINDER identifier-4]
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-DIVIDE]
DIVIDE figure BY 2 GIVING teller.In pascal this statement will look like this:
teller := figure / 2
[This chapter] [TOC] [Tutorial]
| + | for add. |
| - | for subtract. |
| * | for multiply. |
| / | for divide. |
| ** | for raising to a power. |
| ( and ) | for controlling the evaluation. |
COMPUTE identifier-1 [ROUNDED]
[identifier-2 [ROUNDED]]
{EQUAL | =} arithmetical expression
[ON SIZE ERROR imperative-statement-1]
[NOT ON SIZE ERROR imperative-statement-2]
[END-COMPUTE]
COMPUTE teller ROUNDED = (teller + ( figure * 2)) / 4.or
COMPUTE teller ROUNDED EQUAL (teller + ( figure * 2)) / 4.
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
INSPECTThe instruction allows you, one character at a time:EXAMINE
- to tally occurrences in a string
- to replace
- to count and replace
- to convert
The instruction replaces or counts the occurrence of a given character in a variable.STRING
NOT ANSI COBOL-85! Use INSPECT.The instruction allows you to join the contents of an alphanumeric variable.UNSTRINGThis instruction is used to separate the contents of an alphanumeric variable, and to place the parts in different fields.SubstringYou can reference a part of a string, by specifying a start and an end index. This is not an instruction, but a special syntax when you reference strings.TRANSFORMThe instruction replaces a character in an alphanumeric variable, see a converting table.
NOT ANSI COBOL-85! Use INSPECT.
000000* Examples on string-related commands in COBOL-85
*
identification division.
program-id. strings.
author. kik.
*
environment division.
configuration section.
special-names.
console is crt
decimal-point is comma.
*
data division.
working-storage section.
77 Enter-1 pic X(30).
77 Enter-2 pic X(30).
77 disp-line pic 99.
77 strptr pic 999.
* Level 78 is Micro-focus specific
*78 Disp-Length value length of Disp-Field. <* MF, OO-cobol
78 Disp-Length value 64 + 1.
01 Disp-Field.
03 Action pic x(16).
03 Filler pic x.
03 tekst-1 pic x(10).
03 filler pic xxx.
03 tekst-2 pic x(10).
03 filler pic xxx.
03 Result-9 pic 999.
03 filler pic xxx.
03 Result-X.
05 Res-1 pic x(5) just right.
05 Res-2 pic x(5) just right.
05 Res-3 pic x(5) just right.
01 Headers.
03 HAction pic x(16) value "Action".
03 Filler pic x value space.
03 HTekst-1 pic x(10) value "Enter one".
03 filler pic xxx value space.
03 Htekst-2 pic x(10) value "Enter two".
03 filler pic xxx value space.
03 HResult-9 pic x(6) value "Cnt.".
03 HResult-X pic x(10) value "Result".
*
procedure division.
*
main section.
display space.
display "Enter first string:" line 5 position 10.
accept Enter-1 line 5 position 32.
display "Enter second string:" line 6 position 10.
accept Enter-2 line 6 position 32.
if Enter-1 equal space
move "CAaBXAbc" to Enter-1.
if Enter-2 equal space
move "kkkk ii;mm" to Enter-2.
display Headers line 8 position 1.
move 11 to disp-line.
* EXAMINE (OSVS)
perform Prepare-it.
move "Examine (OSVS)" to Action.
move zero to result-9.
examine result-X replacing all "A" by "B".
perform show-it.
* INSPECT (ANS85)
perform Prepare-it.
move "Inspect (ans85)" to Action.
inspect result-X tallying result-9 for all "A".
inspect result-X replacing all "A" by "B".
perform show-it.
* INSPECT (ANS85)
perform Prepare-it.
move "Inspect (ans85)" to Action.
inspect result-X tallying result-9 for all "A".
inspect result-X replacing characters by "."
all "A" by "B"
leading "C" by "D".
perform show-it.
* INSPECT (ANS85)
perform Prepare-it.
move "Inspect (ans85)" to Action.
inspect result-X tallying result-9 for all "A".
inspect result-X replacing
all "A" by "B"
leading "C" by "D"
characters by ".".
perform show-it.
* INSPECT (ANS85)
perform Prepare-it.
move "Inspect (ans85)" to Action.
inspect result-X replacing
all "Abc" by "Xyz"
leading "X" by "Y"
characters by ".".
inspect result-X tallying result-9 for all "Y".
perform show-it.
* INSPECT (ANS85)
perform Prepare-it.
move "Inspect (ans85)" to Action.
inspect result-X converting
"Abc" to "Xyz".
perform show-it.
* STRING (ANS85)
perform Prepare-it.
move "String (ans85)" to Action.
string Enter-1 delimited by space,
Enter-2 delimited by size
into result-X.
perform show-it.
* STRING (ANS85)
perform Prepare-it.
move "String (ans85)" to Action.
move 2 to strptr.
* specifying a substring, from position 3 until end of field.
string Enter-1 (3:) delimited by space,
Enter-2 delimited by size
into result-x
with pointer strptr.
perform show-it.
* UNSTRING (ANS85)
perform Prepare-it.
move "Unstring (ans85)" to Action.
move 2 to strptr.
* specifying a substring, from position 3 until end of field.
unstring Enter-2 (3:) delimited by space or ";" or ","
into res-1, res-2, res-3
with pointer strptr
tallying in result-9.
perform show-it.
stop run.
Prepare-it.
move space to Disp-Field.
move Enter-1 to tekst-1, result-x.
move Enter-2 to tekst-2.
move zero to result-9.
Prepare-it-out.
exit.
*
Show-it.
display Disp-Field line disp-line position 1.
display "<" line disp-line position Disp-Length.
add 1 to disp-line.
Show-it-out.
exit.
---------------------------------------------------------------------------------------------
Enter first string:
Enter second string:
Action Enter one Enter two Cnt. Result
Examine (OSVS) CAaBXAbc kkkk ii;mm 000 CBaBXBbc <
Inspect (ans85) CAaBXAbc kkkk ii;mm 002 CBaBXBbc <
Inspect (ans85) CAaBXAbc kkkk ii;mm 002 ...............<
Inspect (ans85) CAaBXAbc kkkk ii;mm 002 DB...B.........<
Inspect (ans85) CAaBXAbc kkkk ii;mm 000 .....Xyz.......<
Inspect (ans85) CAaBXAbc kkkk ii;mm 000 CXaBXXyz <
String (ans85) CAaBXAbc kkkk ii;mm 000 CAaBXAbckkkk ii<
String (ans85) CAaBXAbc kkkk ii;mm 000 CaBXAbckkkk ii;<
Unstring (ans85) CAaBXAbc kkkk ii;mm 003 k ii mm<
[This chapter] [TOC] [Tutorial]
[This chapter] [TOC] [Tutorial]
- Input data via COBOL ACCEPT
- Calculations
- Numeric editing
- Output data via COBOL DISPLAY.
- Enter two figures
- Calculate the total, difference, product and quotient
- Display the input data and the results of the calculation on the screen, and in a suitable format.