Usages with implicit type
Some USAGE
s in COBOL carry implicit data types:
index
→ binary-int signed, a.k.a. pic S9(9) comp
float
→ float32, a.k.a. comp-1
double
→ float64, a.k.a. comp-2
Additionally, floating point types do not display with leading zeros:
program-id.X.
data division.
local-storage section.
1 i index value 1.
1 j float value 2.
1 k double value 99.
procedure division.
display i
display j
display k
+000000001
2
99
Using array indices to declare variables
If you have an array defined, integer values can be declared as an index to that array.
For example, this (43 chars):
1 a pic 99 occurs 20.
1 i index.
1 j index.
becomes this (33 chars):
1 a pic 99 occurs 20 indexed i j.
All indices declared in this way will additionally be pre-initialized to 1. A single array can have up to 12 indices, according to the documentation.
Declaring multiple arrays at once
If you have two arrays which are or could be the same length, you can declare them both as members of a recurring struct.
For example, this (44 chars):
1 a pic 99 occurs 20.
1 b pic 999 occurs 20.
becomes this (37 chars):
1 occurs 20. 2 a pic 99. 2 b pic 999.
a(8)
would then be accessing the a
of the eighth item, rather than the eighth item of a
.
Inherited usage
If you have multiple variables with the same usage, these can all be declared as part of a single structure.
For example, this (32 chars):
1 a float.
1 b float.
1 c float.
becomes this (23 chars):
1 float. 2 a. 2 b. 2 c.
This will work for index
, float
, and double
usages, but will not work for something like pic 9(9)
, because the implied usage (display
) requires a pic
clause.
Omitting whitespace around numbers
Sometimes, whitespace that surrounds a numeric literal can be omitted.
If to add or subtract a variable,
add -1 to n.
add 1 to n.
can become:
add -1to n.
add+1to n.
This trick can also work to remove the whitespace between two consecutive statements.
For example, this (20 chars):
set i to 1 add+1to n
becomes this (19 chars):
set i to+1add+1to n
Replace
The replace preprocessor directive performs one or more global string substitutions on your source code before executing it.
For example,
program-id.p.
procedure division.
display"a"
display"b"
display"c"
is equivalent to
replace ==!==by==display==.
program-id.p.
procedure division.
!"a"
!"b"
!"c"
This is similar to functionality provided by C's #define
, but there is some unique behavior:
replace ==@==by==pro==.
@gram-id.p.
@cedure division.
display 1
Display without a newline
display"Hello, "with no advancing
display"World!"