• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • A Bevy Of BIFs: %ScanRpl (Scan And Replace)

    September 24, 2014 Jon Paris

    This gem of a BIF was introduced with the V7.1 release and so was not available some five years ago when I wrote my original Bevy series of tips. Now that more people have V7.1 in their shops, I decided it was time to revisit the series and add it to the collection. It is probably my all-time favorite BIF . . . at least so far.

    Simply put, %ScanRpl will search a target string for a given character sequence and replace it with another. Not only that but it will then continue to search through the target string and perform the replacement as many additional times as needed. Prior to the arrival of this new BIF, performing this kind of “mail merge” operation required you to manually code a loop comprising a %Scan and a %Replace operation.

    The basic syntax for the BIF is:

    %SCANRPL(scan string : replacement : source { : scan start
     { : scan length } )
    

    Most of the time you will only need to use the first three parameters to do the job. But as you can see you can also optionally control the search start position and length of the string to be scanned if desired.

    The following code will replace all instances of &Name in the string baseText with the content of the custName variable. The resulting string being placed in the variable outputText.

    outputText = %ScanRpl( '&Name' : custName : baseText );
    

    This example assumes that custName is a variable length field, but if that were not the case we would simply need to trim the parameter like so:

    outputText = %ScanRpl( '&Name': %TrimR( custName ): baseText );
    

    It is important to note that just as with its predecessor, the %Replace BIF, %ScanRpl can replace a short string with a longer one, or vice versa. In fact it can even be used to remove unwanted characters completely from a string by replacing them with a null string. For example, you could easily remove a CRLF sequence at the end of a text line.

    An “Interesting” Example

    I’m not suggesting that the following code is necessarily the best way to achieve the task, but sometimes I like to explore different ways of doing things. When I first saw this technique I thought it would make an interesting example of how %ScanRpl could be used, so here it is.

    Here’s the task. Replace all instances of two or more spaces in a string leaving only single spaces between “words”. The following code will achieve this:

        dcl-s baseString char(38)  
              Inz('4 spc    5 spc     6 spc      2 more  ');
      
        dcl-s result     varChar(38);
    
    (a) result = %ScanRpl( ' ': '<>': baseString ); 
    
        dsply ('Contents: ' + result );
    
    (b) result = %ScanRpl( '><': '': result ); 
    
        dsply ('Contents: ' + result );
    
    (c) result = %ScanRpl( '<>': ' ': result ); 
    
        dsply ('Contents: ' + result );
    
        dsply ('Length of result is ' + %Char(%Len(result)));
    
        *InLr = *On;
    

    As you can see, I have interspersed dsply operations so that when you run the code you can see what happens at each stage. At the first step (a) every single space is replaced by the characters ‘<>’. Next (b) we remove all occurrences of ‘><‘ by replacing them with a null string. At this point, no matter how many spaces we had to start with, we will be left with the string ‘<>’ in their place. So at (c) we replace that string with a single space. That’s all there is to it.

    Jon Paris is one of the world’s most knowledgeable experts on programming on the System i platform. Paris cut his teeth on the System/38 way back when, and in 1987 he joined IBM’s Toronto software lab to work on the COBOL compilers for the System/38 and System/36. He also worked on the creation of the COBOL/400 compilers for the original AS/400s back in 1988, and was one of the key developers behind RPG IV and the CODE/400 development tool. In 1998, he left IBM to start his own education and training firm, a job he does to this day with his wife, Susan Gantner–also an expert in System i programming. Paris and Gantner, along with Paul Tuohy and Skip Marchesani, are co-founders of System i Developer, which hosts the new RPG & DB2 Summit conference. Send your questions or comments for Jon to Ted Holt via the IT Jungle Contact page.

    RELATED STORIES

    A Bevy of BIFs: %Dec to the Rescue

    A Bevy of BIFs: %CHAR, %EDITC and %EDITW

    A Bevy of BIFs: Dealing with a Bad Date

    A Bevy of BIFs: %XLATE and %REPLACE

    A Bevy of BIFs: %SCAN and %CHECK

    A Bevy of BIFs: Getting a Date is Easy with %Date

    A Bevy of BIFs: Look Up to %LookUp



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Midrange Dynamics North America

    With MDRapid, you can drastically reduce application downtime from hours to minutes. Deploying database changes quickly, even for multi-million and multi-billion record files, MDRapid is easy to integrate into day-to-day operations, allowing change and innovation to be continuous while reducing major business risks.

    Learn more.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Profound Logic Software:  October 1 Webinar: "See What i Can Do with Mobile Applications"
    System i Developer:  Upgrade your skills at the RPG & DB2 Summit in Minneapolis, Sept 30 - Oct 2.
    LANSA:  Getting ready to build a mobile app? Webinar: September 24, 12 pm CT/1 pm ET

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ EnterpriseTech: High Performance Computing Industry News From ITJ EIC Timothy Prickett Morgan

    Oracle Teams with Secure-24 for JD Edwards Hosting Cisco’s UCS Mini Enters The Midrange Arena

    Leave a Reply Cancel reply

Volume 14, Number 22 -- September 24, 2014
THIS ISSUE SPONSORED BY:

SEQUEL Software
WorksRight Software
Bug Busters Software Engineering

Table of Contents

  • Testing The Ruby Waters
  • A Bevy Of BIFs: %ScanRpl (Scan And Replace)
  • Admin Alert: What’s The Danger Zone For IBM i Disk Utilization?

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle