COBOL Alphanumeric Data Type Example
Scenario - Declaring alpha-numeric variables, and explaining the data truncation.
Code -
----+----1----+----2----+----3----+----4----+----5----+
       IDENTIFICATION DIVISION.
       PROGRAM-ID. ALPNUMDT.
       AUTHOR. MTH. 
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 WS-VAR.
      * Variable with shorter length than passing data
          05 WS-ALPN-SVAR   PIC X(10).
      * Variable with larger length
          05 WS-ALPN-LVAR   PIC X(20).
       PROCEDURE DIVISION.
           MOVE "MAINFRAME SYSTEMS"   TO  WS-ALPN-SVAR 
                                          WS-ALPN-LVAR.
           DISPLAY "WS-ALPN-SVAR:  -" WS-ALPN-SVAR "-". 
           DISPLAY "WS-ALPN-LVAR:  -" WS-ALPN-LVAR "-". 
           STOP RUN.Output -
WS-ALPN-SVAR (length - 10): -MAINFRAME - WS-ALPN-LVAR (Length - 20): -MAINFRAME SYSTEMS -
Explaining Example -
In the above example:
- "MAINFRAME SYSTEMS" assigns to two alphanumeric variables, WS-ALPN-SVAR and WS-ALPN-LVAR, each with different lengths.
- The string is moving from left to right (the leftmost character moves first because of default justification and rightmost characters are truncated) to the variables.
- First 10 characters moves to the variable to fit the size of WS-ALPN-SVAR and remaining characters truncated.
- The input string completely moved into WS-ALPN-LVAR and the remaing characters filled with spaces at the right side.
- Then, it displays the values of these variables and the program stops its execution.
