Returning variable results with alphanumeric or national functions
The results of alphanumeric or national functions could be of varying lengths and values depending on the function arguments.
About this task
In the following example, the amount of data moved to R3
and
the results of the COMPUTE
statement depend on the
values and sizes of R1
and R2
:
01 R1 Pic x(10) value "e".
01 R2 Pic x(05) value "f".
01 R3 Pic x(20) value spaces.
01 L Pic 99.
. . .
Move Function Max(R1 R2) to R3
Compute L = Function Length(Function Max(R1 R2))
This code has the following results:
R2
is evaluated to be larger thanR1
.- The string 'fbbbb' is moved to
R3
, where b represents a blank space. (The unfilled character positions inR3
are padded with spaces.) L
evaluates to the value 5.
If R1
contained 'g' instead of 'e', the
code would have the following results:
R1
would evaluate as larger thanR2
.- The string 'gbbbbbbbbb'
would be moved to
R3
. (The unfilled character positions inR3
would be padded with spaces.) - The value 10 would be assigned to
L
.
If a program uses national data for function arguments, the lengths and values of the function results could likewise vary. For example, the following code is identical to the fragment above, but uses national data instead of alphanumeric data.
01 R1 Pic n(10) national value "e".
01 R2 Pic n(05) national value "f".
01 R3 Pic n(20) national value spaces.
01 L Pic 99 national.
. . .
Move Function Max(R1 R2) to R3
Compute L = Function Length(Function Max(R1 R2))
This code has the following results, which are similar to the first set of results except that these are for national characters:
R2
is evaluated to be larger thanR1
.- The string
NX"0066 0020 0020 0020 0020"
(the equivalent in national characters of 'fbbbb', where b represents a blank space), shown here in hexadecimal notation with added spaces for readability, is moved toR3
. The unfilled character positions inR3
are padded with national spaces. L
evaluates to the value 5, the length in national character positions ofR2
.
You might be dealing with variable-length output from alphanumeric or national functions. Plan your program accordingly. For example, you might need to think about using variable-length files when the records that you are writing could be of different lengths:
File Section.
FD Output-File Recording Mode V.
01 Short-Customer-Record Pic X(50).
01 Long-Customer-Record Pic X(70).
Working-Storage Section.
01 R1 Pic x(50).
01 R2 Pic x(70).
. . .
If R1 > R2
Write Short-Customer-Record from R1
Else
Write Long-Customer-Record from R2
End-if