1

I am having an issue with coding this, I need some similar code with this but written in RPG to count the digits in a number.

NumField (15, 0) packed decimal

EVAL numDig = %len(%trim(%char(NumField)))

zen
  • 11
  • 2
  • 1
    what is the issue with the code? And you do not need %trim in this case since %char returns a varying string. – RockBoro Sep 16 '22 at 14:39
  • @RockBoro Hi! thanks for the reply. The issue is that there's no built-in function in RPGIII, that written code is from RPGIV and I need a similar code to write in RPGIII – zen Sep 16 '22 at 17:50
  • You can usually convert the code to RPG IV pretty seamlessly with CVTRPGSRC then you can use the code you have. Sticking with RPG III makes no sense anymore as RPG IV came out almost 30 years ago. – Scott Mildenberger Sep 16 '22 at 21:45

2 Answers2

2

the %editc built in function dates back to the begin of time. So does %len, %trim and varying fields.

** ---------------------- test0003r --------------------------- 
dtest0003r        pi                                            
                                                                
d errmsg          s            256a                             
d packNum         s             15p 0                           
d lx              s             10i 0                           
d v20             s             20a   varying                   
d ch50            s             50a                             
 /free                                                          
      packNum     = 32553;                                      
      v20         = %trim(%editc(packNum:'Z')) ;                
      lx          = %len(v20) ;                                 
      ch50        = %trim(%editc(lx:'Z')) + ' ' + v20 ;         
      dsply       ch50 ;                                        
                                                                
      *inlr       = '1' ;                                       
      return ;                                                  
                                                                
 /end-free                                                      
RockBoro
  • 2,163
  • 2
  • 18
  • 34
1

A fun challenge.

@zen, I agree with others. I would not try to code this in RPG III. I would either convert the RPG III program to RPG IV, or I would call an RPG IV program to do this calculation.

But here is some RPG III code that gets the number of digits. It is horrible code, and almost completely untested. I would never use this code or recommend that anyone else use this code.

 C                     Z-ADD1.2       NUM     52
 C                     EXSR SUB1
 C                     Z-ADD-123.45   NUM     52
 C                     EXSR SUB1
 C                     Z-ADD0         NUM     52
 C                     EXSR SUB1
 C                     RETRN
 C           SUB1      BEGSR
 C                     MOVELNUM       STR    30 P
 C           '0':' '   XLATESTR       STR2   30 P
 C           ' '       CHECKSTR2      P       50
 C           P         IFGT 0
 C                     SUBSTSTR2:P    STR3   30 P
 C           ' '       CHEKRSTR3      P       50
 C                     ENDIF
 C           P         DSPLY
 C                     ENDSR

It displays 2, 5, 0.

Barbara Morris
  • 3,195
  • 8
  • 10