2

i was wondering if i could get some examples of the %replace function being used in rpg. My 4th edition programming in rpg IV book just briefly explains this which is leaving me very confused. Im trying to use this function to mask out the first 5 digits in a social security number for my homework assignment. The SSN is already defined in the physical file under "EMPNO" and i will be modifying it in the rpg program before sending that info over to the output file.

What im trying so far,

 D SSN             C                   '***-**-    '  

// The format for the %Replace function is " %REPLACE(newstring:oldstring{:start{:length}})

 EMPNO = %REPLACE(SSN:EMPNO:1:5);   

Which then gives out an error for...

 *RNF0382 30     61 003017  The second parameter EMPNO for %REPLACE is not valid.      
 *RNF7416 30     61 003017  The types of the right and left hand side do not match in the EVAL operation.  

Heres my whole program for any interested and maybe explain a bit better on what im trying to achieve, The program basically just generates a payroll register for hourly employees

  // This program generates a payroll register for Wexler University's hourly employees            
  // from the data file WUHRLYP                                                                                                                                                                    
  // ----------------------------------------------------- Files                                  

 FWUHRLYP   IF   E           K DISK                                                                
 FPROG054PUPO    E             PRINTER OFLIND(*IN99)                                               

  // ----------------------------------------------------- Standalone Variables                   

 D Endofpage       S               N   INZ(*On)                                                    
 D TODAY           S               D   INZ(*Sys)                                                   
 D FULLNAME        S             18                                                                
 D TAXES           S              6  2 INZ(*ZERO)                                                  
 D REGPAY          S              6  2 INZ(*ZERO)
 D OVERTIMERATE    S              6  1 INZ(*ZERO)                                
 D OVERPAY         S              6  2 INZ(*ZERO)                                

   // ----------------------------------------------------- Constants            

 D LOWERCASE       C                   'abcdefghijklmnopqrstuvwxyz'              
 D UPPERCASE       C                   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'                            
 D FEDTAXRATE      C                   Const(.18)                                
 D STATETAXRATE    C                   Const(.05)                                
 D FICARATE        C                   Const(.0751)                              

   // ----------------------------------------------------- Calculation Specs    

  /Free                                                                          
    Read WUHRLYP;                                                                

    Dow Not %Eof(WUHRLYP);                                                       
      IF ENDOFPAGE;                                                              
        WRITE HEADINGS;                                                                                       
        ENDOFPAGE = *OFF;                                  
     ENDIF;                                               

   // CONVERT LNAME TO TO PROPER CASE                     

     LNAME = %XLATE(UPPERCASE:LOWERCASE:LNAME:2);         

   // CONCATENATE THE FIRST INITIAL & LNAME               

     FULLNAME = FNAME + '.' + ' ' + %TRIM(LNAME);         

   // MASK SOCIAL SECURITY NUMBER                         

     EMPNO = %REPLACE(SSN:EMPNO:1:5);                     

   // CALCULATE REGULAR AND OVERTIME PAY THEN GROSS PAY   

     EVAL(H) REGPAY = REGHRS * RATE;                      
     EVAL(H) OVERTIMERATE = RATE + (RATE/2);              
     EVAL(H) OVERPAY = OTHRS * OVERTIMERATE;

   // CALCULATE GROSS PAY, DEDUCTIONS, THEN NET PAY   

     EVAL(H) GROSS = REGPAY + OVERPAY;                
     EVAL(H) FEDERAL = GROSS * FEDTAXRATE;            
     EVAL(H) STATE = GROSS * STATETAXRATE;            
     EVAL(H) FICA = GROSS * FICARATE;                 
     EVAL(H) TAXES = FEDERAL + STATE + FICA;          
     EVAL(H) NET = GROSS - TAXES;                     

   // CALCULATE RUNNING TOTAL OF PAY AND TAXES        

     GROSSTOT += GROSS;                               
     FEDTOT += FEDERAL;                               
     STATETOT += STATE;                               
     FICATOT += FICA;                                 
     NETTOT += NET;                                   

   // POPULATE FINITIAL VARIABLE                      
     FINITIAL = FNAME;         

   // WRITE DETAIL             

     WRITE DETAIL;             

   // ZERO OUT VARIABLES       

     GROSS = *ZEROS;           
     FEDERAL = *ZEROS;     
        STATE = *ZEROS;          
   FICA = *ZEROS;           
   NET = *ZEROS;            
   REGPAY = *ZEROS;         
   OVERPAY = *ZEROS;        
   OVERTIMERATE = *ZEROS;   
   TAXES = *ZEROS;          

 // READ NEXT RECORD        
   READ WUHRLYP;            
   ENDDO;                                       

   // WRITE TOTALS                              

   IF ENDOFPAGE;                                
     WRITE HEADINGS;                            
     ENDOFPAGE = *OFF;                          
   ENDIF;                                       
     WRITE TOTALS;                              

   // CLOSE DATA FILE AND CLEAR UP RESOURCES    

     *INLR = *ON;                               
     RETURN;                                    

 /End-Free

I tried to format the code as best i can, RPG can be a little finicky. Thanks in advanced guys!

Answer

Sorry for the late replay guys been very busy but i finally figured it out. Turns out that EMPNO was a numeric variable and i had to set a variable 'SSN' to be a character and set SSN = to EMPNO. Thanks for all the help guys and heres the new code incase anybody else stubles upon this question.

New variable

D SSN             S             11A

Code for the %REPLACE

// MASK SOCIAL SECURITY NUMBER

  SSN = %CHAR(EMPNO);               

  SSN = %REPLACE('***-**-':SSN:1:5);
mike
  • 1,233
  • 1
  • 15
  • 36
MotoTurbo
  • 23
  • 5
  • I think you'd be better off using substring.... %subst(empno:1:6) = 'xxx-xx' You have SSN declared as a constant that is 12 characters. – bvstone Nov 17 '15 at 20:13
  • 1
    Looking over the %subst section, it seems so simple to code the SSN this way. Would i still need to declare a new variable for the new SSN. I took my SSN constant out by the way. Getting a "The first parameter EMPNO for %SUBST is not valid; built-in function is ignored." Error – MotoTurbo Nov 17 '15 at 21:16
  • What was the result after fixing the error described by your error message ID RNF0382? – user2338816 Nov 18 '15 at 01:50
  • Try things out, Moto... you'll figure it out. We learn by doing! Don't forget to mark this as an answer if it helped. :) – bvstone Nov 18 '15 at 03:56
  • 1
    The question asked for `some examples` but it seems to ignore the ones in the [ILE RPG Language Reference](http://www-01.ibm.com/support/knowledgecenter/ssw_ibm_i_61/rzasd/sc092508877.htm%23bbrepl?lang=en), or at least it doesn't say what's wrong with them. Perhaps never seen? The Language Reference is the first place to look for anything that's not clear in a "book". – user2338816 Nov 19 '15 at 22:21
  • Idk what you just said. I was asking for examples cuz there's barely any actual RPG code online especially from a beginners stand point. – MotoTurbo Nov 20 '15 at 23:31
  • 1
    The link in my comment goes to eight examples. It's not clear what is wrong with them. We can't give good examples until we know what is wrong with the standard examples from IBM. We would waste time just giving the same bad examples that wouldn't help. Are the IBM examples hard to understand? – user2338816 Nov 21 '15 at 03:15

1 Answers1

2

From the error messages, it sounds like EMPNO is a numeric variable. The second parameter for %REPLACE and the first parameter for %SUBST have to be string types (A, C, or G).

If EMPNO is numeric, you can only put numbers in it.

Barbara Morris
  • 3,195
  • 8
  • 10