Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Right Justifying a string

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
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    Post subject: Right Justifying a string
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: 6967
Location: porcelain throne

PostPosted: Fri Jul 03, 2009 8:24 pm    Post subject:
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: 1228
Location: Israel

PostPosted: Sat Jul 04, 2009 7:24 pm    Post subject:
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: 8057
Location: East Dubuque, Illinois, USA

PostPosted: Sat Jul 04, 2009 7:59 pm    Post subject:
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    Post subject:
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Fileaid Update Replace with null string descann Compuware & Other Tools 4 Mon May 15, 2017 3:00 pm
No new posts Replacing same string with different ... vickey_dw DFSORT/ICETOOL 6 Wed Feb 22, 2017 10:44 pm
No new posts Count Trailing Spaces in variable str... Virendra Shambharkar SYNCSORT 10 Thu Feb 02, 2017 12:23 pm
This topic is locked: you cannot edit posts or make replies. Checking wether a string starts from ... sundaram.naveen CLIST & REXX 9 Thu Oct 06, 2016 11:56 am
No new posts Random Password (in string format) ge... ezhavendhan COBOL Programming 10 Mon Aug 29, 2016 3:18 pm


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us