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);