Example: assigning values to COBOL variables

The examples for the COMPUTE, MOVE, and SET commands use the declarations defined in the following COBOL program segment.
01  GRP.
    02  ITM-1 OCCURS 3 TIMES INDEXED BY INX1.
        03  ITM-2 PIC 9(3) OCCURS 3 TIMES INDEXED BY INX2.
01  B.
    02  A     PIC 9(10).
01  D.
    02  C     PIC 9(10).
01  F.
    02  E     PIC 9(10)     OCCURS 5 TIMES.
77     AA          PIC X(5)     VALUE 'ABCDE'.
77     BB          PIC X(5).
        88         BB-GOOD-VALUE VALUE 'BBBBB'.
77     XX          PIC 9(9)    COMP.
77     ONE         PIC 99       VALUE 1.
77     TWO         PIC 99       VALUE 2.
77     PTR         POINTER.
Assign the value of TRUE to BB-GOOD-VALUE. Only the TRUE value is valid for level-88 receivers. For example:
SET BB-GOOD-VALUE TO TRUE;

Assign to variable xx the result of the expression (a + e(1))/c * 2.

COMPUTE xx =(a + e(1))/c * 2;

You can also use table elements in such assignments as shown in the following example:

COMPUTE itm-2(1,2)=(a + 1)/e(2); 

The value assigned to a variable is always assigned to the storage for that variable. In an optimized program, a variable might be temporarily assigned to a register, and a new value assigned to that variable might not alter the value used by the program.

Assign to the program variable c , found in structure d , the value of the program variable a , found in structure b:

MOVE a OF b TO c OF d;

Note the qualification used in this example.

Assign the value of 123 to the first table element of itm-2:

MOVE 123 TO itm-2(1,1);

You can also use reference modification to assign values to variables as shown in the following two examples:

MOVE aa(2:3)TO bb;
MOVE aa TO bb(1:4);

Assign the value 3 to inx1, the index to itm-1:

SET inx1 TO 3;

Assign the value of inx1 to inx2:

SET inx2 TO inx1;

Assign the value of an invalid address (nonnumeric 0) to ptr:

SET ptr TO NULL;

Assign the address of XX to ptr:

SET ptr TO ADDRESS OF XX;

Assigns the hexadecimal value of X'20000' to the pointer ptr:

SET ptr TO H'20000';