using %REPLACE to Scan Replace in RPG4

  • Posted on: 25 October 2012
  • By: NickLitten

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.

So, to change this to work for you

  1. Change the value of L#EMAIL to the value that you want to be replaced
  2. Change the value of SRCDTA to the field name that you want to scan
  3. Change the value of PARMEMAIL to the new value that you want to replace every occurrence of "L#EMAIL"

Hope it helps...


If you are using the PROJEX4I Power Tools:

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:

 = #Scanreplace (  : 'bob' : 'Fred' )

Much simpler than using the complex %REPLACE BIF I'm sure you will agree. 

Here's the Prototype:

 //  +-------------------------------------------------------------------+      
 //  | #ScanReplace - Scan a string and replace stuff in it              |      
 //  +-------------------------------------------------------------------+      
 /if defined(#ScanReplace)                                                      
P #ScanReplace    B                   EXPORT                                    
d #ScanReplace    PI                  Like(String)                        
d ParmSource                          Like(String)                        
d                                     const                                     
d OldString                   1024A   Const varying                             
d NewString                   1024A   Const varying                             
 // local variables *                                                           
d $ScanReplace    S                   Like(String)   
d $Found          S             10I 0                      
d $Replace        S             10I 0                                                   
   = ParmSource;                                                    
   = %scan(%trimr(OldString):);                               
  dow  > 0;                                                               
     = %replace(%trimr(NewString):                      

     =  + %len(NewString);                       
     = %scan(%trimr(OldString)::);   
  Return ;                                         
p                 e                                            

Obviously you will also need the Procedure which looks like this:

 //  +--------------------------------------------------------+   
 //  | #ScanReplace - Scan the string and replace stuff in it |   
 //  +--------------------------------------------------------+   
 /if not defined(#ScanReplace)
d  String         S           1000    varying                                                     
d #ScanReplace    PR                  Like()          
d ParmSource                          Like()          
d                                     const                       
d OldString                   1024A   Const varying               
d NewString                   1024A   Const varying               
 /define #ScanReplace