Reading — step 1 of 5
Learn
~2 min readArithmetic and Editing
MOVE is COBOL's universal assignment. Its behavior depends on the target's PIC.
Number to number
01 A PIC 9(5) VALUE 12345.
01 B PIC 9(3).
MOVE A TO B. *> B = '345' (truncated from right)
01 C PIC 9(7).
MOVE A TO C. *> C = '0012345' (zero-padded)
Numbers align right. Truncation drops leading digits; padding adds leading zeros.
String to string
01 X PIC X(10) VALUE "Ada Lovelace".
01 Y PIC X(5).
MOVE X TO Y. *> Y = 'Ada L' (left-truncated)
01 Z PIC X(15).
MOVE X TO Z. *> Z = 'Ada Lovelace ' (right-padded with spaces)
Strings align left. Truncation drops trailing characters; padding adds trailing spaces.
MOVE between types
01 NUM PIC 9(3) VALUE 42.
01 TXT PIC X(5).
MOVE NUM TO TXT. *> TXT = '042 ' (numeric becomes string of digits)
INITIALIZE
01 RECORD-IN.
05 NAME PIC X(20).
05 AGE PIC 9(3).
05 SALARY PIC 9(7)V99.
INITIALIZE RECORD-IN.
*> NAME = ' ' (20 spaces)
*> AGE = '000'
*> SALARY = '0000000.00'
Resets every field to its type's default — spaces for strings, zeros for numbers.
Selective:
INITIALIZE RECORD-IN REPLACING NUMERIC BY ZEROS
ALPHANUMERIC BY SPACES.
VALUE clause
Initial values at declaration:
01 COUNTER PIC 9(5) VALUE 0.
01 STATUS PIC X(10) VALUE 'PENDING'.
01 PRICES.
05 P PIC 9(5) OCCURS 3 TIMES VALUE 100. *> all three are 100
Special figurative constants
SPACES— fill with spacesZEROS/ZEROES— fill with zerosLOW-VALUES— fill with\x00(binary low)HIGH-VALUES— fill with\xFF(binary high)QUOTES— fill with"characters
MOVE SPACES TO NAME.
MOVE ZEROS TO COUNTER.
IF FIELD = LOW-VALUES THEN ...
MOVE CORRESPONDING
For groups with same-named subfields:
01 FROM-PERSON.
05 NAME PIC X(20).
05 AGE PIC 9(3).
05 EMAIL PIC X(40).
01 TO-PERSON.
05 NAME PIC X(20).
05 AGE PIC 9(3).
MOVE CORRESPONDING FROM-PERSON TO TO-PERSON.
*> Copies NAME and AGE; ignores EMAIL (not in destination)
Useful for record-to-record copies. Saves listing every field.
MOVE is the workhorse — every COBOL program is full of them. Get familiar with the truncation/padding rules; they bite if you're not paying attention.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…