.st0{fill:#FFFFFF;}

using %REPLACE to Scan Replace in RPG 

 October 25, 2012

By  NickLitten

UPDATE 2017 : USE %SCANRPL Built in Function – IBM added a %BIF to do this for us in modern RPGLE so we can now just do a %SCANRPL in native RPG. I’m leaving this old blog here for reference but look HERE for the modern and much much easier way of doing this schnizzle

So, You still want to see how to SCAN REPLACE in RPG the old fashioned way?

What’s the easiest way?

Using the %REPLACE built in Function is easy and efficient although 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.

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:

myVariable = Scan_Replace ( : 'bob' : 'Fred' )

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

Here’s the Prototype:

 // +-------------------------------------------------------------------+ 
 // |  Scan_Replace - Scan a string and replace stuff in it |
 // +-------------------------------------------------------------------+ 
 dcl-proc Scan_Replace export;
 dcl-pi Scan_Replace like($bigvariable);
   ParmSource like($bigvariable) const;
   OldString varchar(1024) const;
   NewString varchar(1024) const;
 end-pi;
 
 dcl-s $ScanReplace like($bigvariable);
 dcl-s $Found int(10);
 dcl-s $Replace int(10);
 
 $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-proc;

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

 // +--------------------------------------------------------+
 // |  Scan_Replace - Scan the string and replace stuff in it |
 // +--------------------------------------------------------+
 dcl-pr Scan_Replace like($bigvariable) extproc(*cl:'SCAN_REPLACE');
   *n like($bigvariable) const; // ParmSource
   *n varchar(1024) const; // OldString
   *n varchar(1024) const; // NewString
 end-pr;

NickLitten


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"cff50":{"name":"Main Accent","parent":-1},"a344d":{"name":"Accent Transparent","parent":"cff50"}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"cff50":{"val":"var(--tcb-skin-color-0)"},"a344d":{"val":"rgba(46, 138, 229, 0.85)","hsl_parent_dependency":{"h":210,"l":0.54,"s":0.78}}},"gradients":[]},"original":{"colors":{"cff50":{"val":"rgb(0, 178, 255)","hsl":{"h":198,"s":1,"l":0.5}},"a344d":{"val":"rgba(0, 178, 255, 0.85)","hsl_parent_dependency":{"h":198,"s":1,"l":0.5}}},"gradients":[]}}]}__CONFIG_colors_palette__

Get In Touch

I’m always looking for awesome input, feedback and critique!

>