using %REPLACE to Scan Replace in RPG4

replace my head with a younger more handsoe one please

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) 
              :SrcDta 
              :%Scan('L#EMAIL' : %TRIMR(SrcDta ))
              :7);    
EndDo;                                                   

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:

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

    $Replace = $Found + %len(NewString);                       
    $Found = %scan(%trimr(OldString):$ScanReplace:$Replace);   
  enddo;                                                       
  Return $ScanReplace;                                         
 /end-free                                                     
p                 e                                            
 /endif                                                        

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 #ScanReplace    PR                  Like($BigVariable)          
d ParmSource                          Like($BigVariable)          
d                                     const                       
d OldString                   1024A   Const varying               
d NewString                   1024A   Const varying               
 /define #ScanReplace                                             
 /endif                                                           
Tag Cloud: