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
Bill Woodger mentioned: "There are also ... byte-by-byte MOVEs (sometimes preferred by "traditionalists", but the least clear of the lot)." Here is one such solution that I hope is reasonably clear. Below are two version. First, a solution updated for the suggestions below. Second, the original solution proposed, to reference in understanding the comments made. Thank you, Bill, for your good suggestions! I might learn to write good COBOL one of these days :)
IDENTIFICATION DIVISION.
PROGRAM-ID. SHIFT-RIGHT.
* SITUATION ON STACKOVERFLOW.COM:
* 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
* LEFT AND PUT THE NUMBER OF SPACES WITH '0' AT THE BEGINING.
* THIS EXAMPLES SPEAKS FOR THEMSELVES:
* INPUT OUTPUT
* ==============================
* '123456 ' -> '0000123456'
* '12345678 ' -> '0012345678'
* '123456789 ' -> '0123456789'
* '1234567890' -> '1234567890'
*
* ASSUME INPUT DATA VALIDATION DONE ELSEWHERE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 STRING-IN PIC X(10).
01 STRING-OUT PIC X(10).
01 STRING-LENGTH PIC 99 USAGE COMP.
01 CHAR-IN-NUM PIC 99 USAGE COMP.
01 CHAR-OUT-NUM PIC 99 USAGE COMP.
PROCEDURE DIVISION.
MAIN.
PERFORM INITIALIZE-LENGTH
MOVE '123456 ' TO STRING-IN
PERFORM MAKE-AND-SHOW
MOVE '12345678 ' TO STRING-IN
PERFORM MAKE-AND-SHOW
MOVE '123456789 ' TO STRING-IN
PERFORM MAKE-AND-SHOW
MOVE '1234567890' TO STRING-IN
PERFORM MAKE-AND-SHOW
MOVE SPACES TO STRING-IN
PERFORM MAKE-AND-SHOW
STOP RUN
.
INITIALIZE-LENGTH.
MOVE LENGTH OF STRING-IN TO STRING-LENGTH
IF LENGTH OF STRING-OUT NOT = STRING-LENGTH
DISPLAY 'LENGTH OF STRING-IN, ' STRING-LENGTH ', '
'NOT EQUAL TO LENGTH OF STRING-OUT, '
LENGTH OF STRING-OUT
STOP RUN
END-IF
.
MAKE-AND-SHOW.
PERFORM MAKE-STRING-OUT
PERFORM SHOW-BEFORE-AFTER
.
MAKE-STRING-OUT.
MOVE ZEROS TO STRING-OUT
MOVE STRING-LENGTH TO CHAR-OUT-NUM
CHAR-IN-NUM
PERFORM STRING-LENGTH TIMES
IF STRING-IN (CHAR-IN-NUM:1) NOT = SPACE
MOVE STRING-IN (CHAR-IN-NUM:1)
TO STRING-OUT (CHAR-OUT-NUM:1)
SUBTRACT 1 FROM CHAR-OUT-NUM
END-IF
SUBTRACT 1 FROM CHAR-IN-NUM
END-PERFORM
.
SHOW-BEFORE-AFTER.
DISPLAY "STRING IN: '" STRING-IN "'"
DISPLAY "STRING OUT: '" STRING-OUT "'"
DISPLAY " "
.
**********************************************
* EARLIER VERSION, BEFORE IMPROVEMENTS
* SUGGESTED IN COMMENTS
**********************************************
IDENTIFICATION DIVISION.
PROGRAM-ID. SHIFT-LEFT.
* SITUATION ON STACKOVERFLOW.COM:
* 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
* LEFT AND PUT THE NUMBER OF SPACES WITH '0' AT THE BEGINING.
* THIS EXAMPLES SPEAKS FOR THEMSELVES:
* INPUT OUTPUT
* ==============================
* '123456 ' -> '0000123456'
* '12345678 ' -> '0012345678'
* '123456789 ' -> '0123456789'
* '1234567890' -> '1234567890'
*
* ASSUME INPUT DATA VALIDATION DONE ELSEWHERE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 STRING-IN PIC X(10).
01 STRING-OUT PIC X(10).
01 CHAR-IN-NUM PIC 99 USAGE COMP-3.
01 CHAR-OUT-NUM PIC 99 USAGE COMP-3.
PROCEDURE DIVISION.
MAIN.
MOVE '123456 ' TO STRING-IN
PERFORM MAKE-AND-SHOW
MOVE '12345678 ' TO STRING-IN
PERFORM MAKE-AND-SHOW
MOVE '12345678 ' TO STRING-IN
PERFORM MAKE-AND-SHOW
MOVE '123456789 ' TO STRING-IN
PERFORM MAKE-AND-SHOW
MOVE '1234567890' TO STRING-IN
PERFORM MAKE-AND-SHOW
STOP RUN
.
MAKE-AND-SHOW.
PERFORM MAKE-STRING-OUT
PERFORM SHOW-BEFORE-AFTER
.
MAKE-STRING-OUT.
MOVE SPACES TO STRING-OUT
MOVE 10 TO CHAR-OUT-NUM
PERFORM VARYING CHAR-IN-NUM FROM 10 BY -1
UNTIL CHAR-IN-NUM < 1
IF STRING-IN (CHAR-IN-NUM:1) NOT = SPACE
MOVE STRING-IN (CHAR-IN-NUM:1)
TO STRING-OUT (CHAR-OUT-NUM:1)
SUBTRACT 1 FROM CHAR-OUT-NUM
END-IF
END-PERFORM
PERFORM UNTIL CHAR-OUT-NUM < 1
MOVE ZERO TO STRING-OUT (CHAR-OUT-NUM:1)
SUBTRACT 1 FROM CHAR-OUT-NUM
END-PERFORM
.
SHOW-BEFORE-AFTER.
DISPLAY "STRING IN: '" STRING-IN "'"
DISPLAY "STRING OUT: '" STRING-OUT "'"
DISPLAY " "
.