TechTip: New in 7.1: The %SCANRPL Built-in Function

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Replacing values has gotten a whole lot easier!

 

The new RPG IV built-in function %SCANRPL gives you the ability to scan a string for a value and then replace all occurrences with another value. The first parameter is the string you want to search for within another string. The second parameter is the string you want to replace it with. The third parameter is the string you want to scan. Simple.

 

This is pretty handy when we compare the way things used to be in the example below. You've got to find start positions, get lengths of strings, and set up loops to check for multiple instances of the same string. Also, a number of functions are called here—like %len, %scan, and %replace. It's all a bit messy and hard to read...and the first code example below is in free-format! I'm wondering what the equivalent code looks like in fixed-format RPG III...probably about as pretty as a well-lit exhibit of National Hockey League dental X-rays..

 

D search1         C                   'iSeries.'

D search2         C                   'OS/400'

D rep1            C                   'Power Systems!'

D rep2            C                   'IBM i'

D pos             s             10i 0

 

/free

string = 'I love OS/400 on iSeries.';

   pos = %scan(search1: string);                                

   dow pos > 0;                                                   

      string = %replace(rep1: string: pos: %len(search1));

      if (pos+%len(rep1) > %len(string));

         pos = 0;

      else;

         pos = %scan(search1: string: pos+%len(rep1));          

      endif;

   enddo;

 

pos = %scan(search2: string);                                

   dow pos > 0;                                                   

      string = %replace(rep2: string: pos: %len(search2));

      if (pos+%len(rep2) > %len(string));

         pos = 0;

      else;

         pos = %scan(search2: string: pos+%len(rep2));            

      endif;

   enddo;

 

 //string = 'I love IBM i on Power Systems!'

/end-free

 

Instead, by using the %SCANRPL built-in function, we're able to cut out every loop, variable, if, and else and produce a very simple few lines of code to achieve the same results.

 

Behold! 

 

/free

 

string1 = 'I love OS/400 on iSeries.';

string2 = %ScanRpl('iseries.' : 'Power Systems!' : string1);

string3 = %ScanRpl('OS/400' : 'IBM i' : string2);

 

// string3 = 'I love IBM i on Power Systems!'

 

/end-free

 

The optional last two parameters of %SCANRPL let you specify the start and end positions of the string you want to scan, so you don't need to replace all occurrences of the pattern if you don't want to.

 

Now that, my fellow string-manipulating friends, is cool.

 

as/400, os/400, iseries, system i, i5/os, ibm i, power systems, 6.1, 7.1, V7, V6R1

Steve Pitcher
Steve Pitcher works with iTech Solutions, an IBM Premier Business Partner. He is a specialist in IBM i and IBM Power Systems solutions since 2001. Feel free to contact him directly This email address is being protected from spambots. You need JavaScript enabled to view it..
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: