CASE 2 & 3:
COBOL statements: MOVE WS-INPUTTO WS-OUTPUT.DISPLAY “ OUTPUT IS : “ WS-OUTPUT.
ERROR!
The reason being there arespacesin the input data, which are Characters.And we cannot move characters into a numeric field.
The simple
MOVE statement will not work. Now the Question arises what to do if we need the numeric values of analphanumeric variable?
The above result can be achieved in 3 ways:
1.
Using an array.
2.
Using INSPECT statement.
3.
NUMVAL () Function.
1.
Using an array:
01 WS-ARR.05 WS-INPUT-ARR OCCURS 10 TIMESPIC X (1).01 WS-IDXPIC 9(02) VALUE ZEROES.MOVE WS-INPUT TO WS-ARR.PERFORM VARYING WS-IDX FROM 1 BY 1 UNTIL WS-INOUT-ARR (WS-IDX) ISNOT NUMERIC.MOVE WS-INPUT (1:WS-IDX) TO WS-OUTPUT. DISPLAY “ OUTPUT IS : “ WS-OUTPUT. Result for INPUT RECORD 2:OUTPUT IS : 0000010107 Result for INPUT RECORD 3:OUTPUT IS : 0000101006
This particular method is not considered a good programming practice.
2.
Using INSPECT Statement:
*************************************************************** IT COUNTS THE NUMBER CHARACTERS BEFORE THE FIRST SPACE ** IS ENCOUNTERED AND MOVES THE VALUE OF THAT COUNT TO WS-IDX ***************************************************************