ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Easiest way to right-adjust

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Easiest way to right-adjust

    Anybody got an easy way to right-adjust a character variable with zero fill? I have been using perform-until loops, but wondered if there is easier or better method that I'm overlooking.

    Here's an example. Some-var is defined as PIC X(6).

    Code:
    perform until Some-var (6:1) not equal space
        move Some-var (1:5)  to Some-var (2:5)    
        move zero to Some-var (1:1)    
    end-perform

  • #2
    Re: Easiest way to right-adjust

    It took a minute to see what your code was doing. Not bad, though a little misleading (to me) at first glance.

    Here are a couple alternatives. The example using INSPECT is perhaps more recognized by COBOL developers.

    Code:
           WORKING-STORAGE SECTION.
    
           77  szChars                     pic  9(9) comp-4.
           77  offset                      pic  9(9) comp-4.
    
           77  base0                       pic  x(20) value zero.
    
           77  target1 like base0.
           77  target2 like base0          justified.
    
           01  myChars                     pic  x(20) value 'ABCDE'.
    
           Procedure Division.
           00-Main-driver.
    
          *  Use reference modification...
               move    base0       to  target1
    
               compute szChars     =
                       FUNCTION LENGTH( FUNCTION TRIM( myChars ))
               compute offset      =   LENGTH OF target1 - szChars + 1
    
               move    myChars ( 1 : szChars )
                                   to  target1 ( offset : szChars )
    
          *  Use JUSTIFIED/INSPECT
               move    FUNCTION TRIM( myChars )
                                   to  target2
               inspect target2 REPLACING LEADING space BY zero
    
               goback.
    Unusual request. I'd normally think we'd be working with numeric values so that leading zeros were automatic. I used VALUE 'ABCDE' to show that leading zeros might be generated whether for numerics or non-numerics.
    Tom

    There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

    Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

    Comment


    • #3
      Re: Easiest way to right-adjust

      Originally posted by tomliotta View Post
      Unusual request. I'd normally think we'd be working with numeric values so that leading zeros were automatic.
      These are old mainframe programs. All input fields are defined as alphanumeric and have to be converted to numeric. With the increasing use of XML, JSON, etc., I see more and more need to convert character values to numeric.

      Comment

      Working...
      X