IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Right Justifying a string


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
ranisupriya

New User


Joined: 03 Jul 2009
Posts: 3
Location: bangalore

PostPosted: Fri Jul 03, 2009 8:05 pm
Reply with quote

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
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Fri Jul 03, 2009 8:24 pm
Reply with quote

prefill with zeros and use reference modification. rules and explantion of reference modification
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Sat Jul 04, 2009 7:24 pm
Reply with quote

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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8697
Location: Dubuque, Iowa, USA

PostPosted: Sat Jul 04, 2009 7:59 pm
Reply with quote

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
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Sat Aug 01, 2009 5:24 am
Reply with quote

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
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts Sortjoin and Search for a String and ... DFSORT/ICETOOL 1
No new posts Quantifying/Justifying CPU savings All Other Mainframe Topics 2
No new posts file manager is doing string conversion IBM Tools 3
Search our Forums:

Back to Top