Example: arithmetic expressions as reference modifiers
Suppose that a field contains some right-justified characters,
and you want to move those characters to another field where they
will be left justified. You can do so by using reference modifiers
and an INSPECT
statement.
Suppose a program has the following data:
01 LEFTY PIC X(30).
01 RIGHTY PIC X(30) JUSTIFIED RIGHT.
01 I PIC 9(9) USAGE BINARY.
The program counts the number of leading spaces and, using arithmetic expressions in a reference modifier, moves the right-justified characters into another field, justified to the left:
MOVE SPACES TO LEFTY
MOVE ZERO TO I
INSPECT RIGHTY
TALLYING I FOR LEADING SPACE.
IF I IS LESS THAN LENGTH OF RIGHTY THEN
MOVE RIGHTY ( I + 1 : LENGTH OF RIGHTY - I ) TO LEFTY
END-IF
The MOVE
statement transfers characters from RIGHTY
,
beginning at the position computed as I + 1 for a length that is computed
as LENGTH OF RIGHTY - I
, into the field LEFTY
.