JUSTIFIED Clause
JUSTIFIED Clause
- By default, alphabetic and alphanumeric data in the COBOL variables are left-aligned, and numeric data is right-aligned.
- JUSTIFIED | JUST clause overrides the default alignment of non-numeric data types. It aligns the content to the right while displaying them.
- ItĀ applies only to alphabetic and alphanumeric variables but not numeric ones. Because the numeric data type is always right-aligned, it has nothing to do with it.
Syntax -
01 ws-var PIC X(var-length) [JUSTIFIED|JUST] [RIGHT].
Note! All statements coded in [ ] are optional.
For example -
01 WS-VAR PIC X(11) JUSTIFIED RIGHT.
- ws-variable - Specifies the variable name. From example - WS-VAR.
- var-length - Specifies the length of the string to store in the variable. The maximum length is 256 characters. From example - 11.
Process -
- As defined, alphabetic or alphanumeric variables data is left aligned by default.
- If the alphabetic or alphanumeric variables are declared without a JUSTIFIED clause, the data transfer starts from left to right.
i.e., the leftmost byte transfers first and the rightmost byte at the end.
If the receiving variable is smaller than the sending data, the data truncation happens on the right side.
The result value in WS-SM-VAR is "MAINFRA".01 WS-SM-VAR PIC X(07). PROCEDURE DIVISION. MOVE "MAINFRAMES" TO WS-SM-VAR.
- If the alphabetic or alphanumeric variables are declared with a JUSTIFIED clause, the data transfer starts from right to left.
i.e., the rightmost byte transfers first and the leftmost byte at the end.
If the receiving variable is smaller than the sending data, the data truncation happens on the left side.
The result value in WS-SM-VAR is "NFRAMES".01 WS-RJL-VAR PIC X(07) JUSTIFIED RIGHT. PROCEDURE DIVISION. MOVE "MAINFRAMES" TO WS-RJL-VAR.
- If the sending item is smaller than the receiving item, the unused character positions are filled with spaces.
Rules -
JUSTIFIED clause can't be used for -
- Numeric, numeric-edited, or alphanumeric-edited variables.
- Index variables.
- Floating-point variables.
- Level-66 (RENAMES) and level-88 (condition-name) variables.
- JUSTIFIED clause can code only at the elementary level.
Practical Example -
Scenario - Below example describes how the JUSTIFIED clause used in COBOL programming.
Code -
----+----1----+----2----+----3----+----4----+----5----+
...
WORKING-STORAGE SECTION.
01 WS-VAR.
05 WS-VAR1 PIC X(15).
05 WS-SM-VAR PIC X(07).
05 WS-RJE-VAR PIC X(15) JUSTIFIED RIGHT.
05 WS-RJL-VAR PIC X(07) JUSTIFIED RIGHT.
...
PROCEDURE DIVISION.
MOVE "MAINFRAMES" TO WS-VAR1
WS-SM-VAR
WS-RJE-VAR
WS-RJL-VAR.
DISPLAY "WS-VAR1 :" WS-VAR1 ":".
DISPLAY "WS-SM-VAR :" WS-SM-VAR ":".
DISPLAY "WS-RJE-VAR :" WS-RJE-VAR ":".
DISPLAY "WS-RJL-VAR :" WS-RJL-VAR ":".
...
Output -
WS-VAR :MAINFRAMES : WS-SM-VAR :MAINFRA: WS-RJE-VAR : MAINFRAMES: WS-RJL-VAR :NFRAMES:
Note! In the above example, ":" is used in the output to showcase the variable length, including spaces.