So, I want to do a SCAN and REPLACE in RPG - What's the easiest way?
Using the %REPLACE built in Function is easy and efficient allthough code wise its a little cumbersome:
// Update email address
Dow %Scan('L#EMAIL': %TRIMR(SrcDta )) > *Zeros;
SrcDta = %Replace(%trimr(ParmEmail)
:%Scan('L#EMAIL' : %TRIMR(SrcDta ))
In this example, I am scanning a program variable (SRCDTA) looking for any occurence of the word "L#EMAIL" and replacing it with the contents of "PARMEMAIL". The email value is %TRIM'ed which means that any blanks at the end of the email address are ignored.
I use a similar process in the PROJEX Service Program and then you can easily run a SCAN/REPLACE on a single line of RPG code.
So lets say I wanted to scan for the word 'bob' and change it for the word 'Fred' within a string i could do it like this using this Prototype:
$ResultVariable = #Scanreplace ( $OriginalVariable : 'bob' : 'Fred' )
Much simpler than using the complex %REPLACE BIF I'm sure you will agree.
Here's the Prototype: