View previous topic :: View next topic
|
Author |
Message |
ranisupriya
New User
Joined: 03 Jul 2009 Posts: 3 Location: bangalore
|
|
|
|
I want to move a string whose length is less than or equal to 9 to a string of lenghth 10. Now I have to rightjustify it and pad the leading position with 0's. could anyone suggest me how to do this. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
Back to top |
|
|
Marso
REXX Moderator
Joined: 13 Mar 2006 Posts: 1353 Location: Israel
|
|
|
|
If you right justify and pad to 0, then you must talk about numbers...
Using PIC 9(10) is not an option?
If not, a small example would be useful. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8697 Location: Dubuque, Iowa, USA
|
|
|
|
Have you looked at JUST RIGHT in the COBOL Language Reference manual (link at the top of the page)? JUST RIGHT and INSPECT REPLACING LEADING SPACE BY ZERO should do exactly what you say you want. |
|
Back to top |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
Couldn't find a forum named "handy tips" so here's some code to right justify the contents of any field. I realize there are several ways to accomplish this but here's my contribution:
Code: |
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
05 WS-ANYFIELD PIC X(??) VALUE 'XXX'.
05 WS-WORK-FIELD PIC X(100).
* LENGTH of WS-WORK-FIELD must be >=
* WS-LENGTH-OF-ORIG-FIELD
05 WS-LENGTH-OF-ORIG-FIELD PIC 999.
05 WS-NBR-OF-TRAILING-BLANKS PIC 999.
05 WS-LENGTH-OF-DATA PIC 999.
05 WS-START-POS PIC 999.
* The following will right justify the contents of
* WS-ANYFIELD.
IF WS-ANYFIELD = SPACE
OR WS-ANYFIELD (LENGTH OF WS-ANYFIELD:1) NOT = SPACE
CONTINUE
ELSE
MOVE WS-ANYFIELD TO WS-WORK-FIELD
MOVE LENGTH OF WS-ANYFIELD TO WS-LENGTH-OF-ORIG-FIELD
PERFORM XXXX-RIGHT-JUSTIFY
MOVE SPACE TO WS-ANYFIELD
MOVE WS-WORK-FIELD (1:WS-LENGTH-OF-DATA)
TO WS-ANYFIELD (WS-START-POS:WS-LENGTH-OF-DATA)
END-IF
.
* Right justify the contents of WS-WORK-FIELD.
XXXX-RIGHT-JUSTIFY.
MOVE ZERO TO WS-NBR-OF-TRAILING-BLANKS
INSPECT FUNCTION REVERSE (WS-WORK-FIELD)
TALLYING WS-NBR-OF-TRAILING-BLANKS FOR LEADING SPACE
COMPUTE WS-LENGTH-OF-DATA
= LENGTH OF WS-WORK-FIELD - WS-NBR-OF-TRAILING-BLANKS
COMPUTE WS-START-POS
= WS-LENGTH-OF-ORIG-FIELD - WS-LENGTH-OF-DATA + 1
. |
|
|
Back to top |
|
|
|