Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

left justifyng alphanumeric data 2

Status
Not open for further replies.

cobolpmg

Programmer
Feb 10, 2001
1
US
I have a pic x(10) field. The field has 7 leading blanks and the remaining 3 charatactres are numeric ex 123. How can i get 123 in the first 3 positions and blank out the remaining fields?
 
Here is one way to do it...

LEFT-ADJUST-WORK-10.
IF WORK-10(1:1) = SPACE
AND WORK-10 NOT = SPACES
INSPECT WORK-10 TALLYING PTR-1 FOR LEADING SPACES
ADD 1 TO PTR-1
ADD 1 TO ZERO GIVING PTR-2
PERFORM UNTIL PTR-1 > LENGTH OF WORK-10
MOVE WORK-10(PTR-1:1) TO WORK-10(PTR-2:1)
MOVE SPACE TO WORK-10(PTR-1:1)
ADD 1 TO PTR-1
ADD 1 TO PTR-2
END-PERFORM
END-IF.

The WORKING-STORAGE items are defined as follows...

01 WORK-10 PIC X(10) VALUE " 123".
01 PTR-1 PIC 999 VALUE 0.
01 PTR-2 PIC 999 VALUE 0.

Good Luck...
 
Heres another way:
01 yourfield pic x(10).
01 newfield pic x(10) value spaces.
01 spc-count pic 99.

inspect yourfield tallying spc-count for leading spaces.
add 1 to spc-count.
unstring yourfield with pointer spc-count into newfield.
move newfield to yourfield.
 
And here is yet another way.

01 Source-Field pic x(20).
01 Temp-Field Pic X(20).

If source-field > spaces
move source-field to temp-field
perform until temp-field (1:1) > spaces
move source-field (2:) to temp-field
move temp-field to source-field
end-perform
end-if

Another way, uses source-field instead of temp-field and removes the second move in the perform. It works on all COBOL implementations I'm aware of - but it's a little "tricky".

Ie:

Perform until source-field (1:1) > spaces
move source-field (2:) to source-field
end-perform

 
Cobolpmg -
These solutions are all good ways to left justify a right justified field, but if your data really just has a three digit number which is always in positions 8-10, as you describe it, and move it to the first three positions, you can do that with just two move statements:

05 SHIFTY-ITEM PIC X(10).

MOVE SHIFTY-ITEM (8:) TO SHIFTY-ITEM (1: 3).
MOVE SPACES TO SHIFTY-ITEM (8:).

If you really want to have full support shifting from right to left justification, you will have to use one of the other suggestions listed previously. Betty Scherber
Brainbench MVP for COBOL II
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top