I have an alphanumeric variable with a length of 10. It contains a number at the beginning, the rest of the digits are filled with spaces. Then I need to move the string to the
I am surprised no one has suggested using NUMVAL here...
IDENTIFICATION DIVISION.
PROGRAM-ID. EXAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIN PIC X(10).
01 VAROUT PIC 9(10).
PROCEDURE DIVISION.
MOVE '123456 ' TO VARIN
COMPUTE VAROUT = FUNCTION NUMVAL(VARIN)
DISPLAY '>' VARIN '<'
DISPLAY '>' VAROUT '<'
GOBACK
.
which produces...
>123456 <
>0000123456<
The problem with this approach is that if the NUMVAL argument does not convert to
numeric the program throws an exception and dies. Also this is not very
efficient from a CPU usage point of view because it requires character to
binary numeric conversion and back to display format again (all done under the covers but takes cycles).
As a general rule I would not recommend using NUMVAL (even if it looks like a 'nicer' solution). I would stick with the solution as presented in the original question. That solution is computationally efficient and is a common idiom used in COOBL programming.