• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • To Shift or Not to Shift: That Is in the Fourth Parameter

    April 25, 2007 Ted Holt

    RPG’s %REPLACE function is marvelous. When I think of all those array-manipulation calcs I wrote in my System/34 and System/36 days, I could cry. Anyway, to use %REPLACE effectively, it is good to ask oneself a simple question: In the event that the replacement string and the string of characters that is being replaced are not the same length, do I, or do I not, want the characters that follow the replacement to shift?

    This is the same question SEU asks on the Find/Change Options panel. Informing SEU of your desire is an almost effortless matter of typing a Y or an N into the Allow Data Shift field. Informing %REPLACE of your desire is nearly as easy. Just remember what to place in the fourth parameter.

    Here are the four parameters defined for %REPLACE:

    1. the string to be inserted
    2. the string to be modified
    3. the position at which to begin replacing characters
    4. the number of characters to replace

    And here is the rule pertaining to the fourth parameter:

    To shift, place the length of the string to be replaced in the fourth parameter. To avoid the shift, place the length of the replacement string in the fourth parameter, or omit the fourth parameter.

    In the following example of excellence in programming, pet is to be replaced with wallaby in the sentence, “My pet’s name is Harold.”

    D OriginalString  s             64a   varying             
    D FixedRepl       s                   like(OriginalString)
    D ShiftRepl       s                   like(OriginalString)
    D ScanFor         s             20a   varying             
    D ReplaceWith     s             60a   varying             
    D Pos             s              5i 0                     
     /free                                                    
         OriginalString = 'My pet''s name is Harold.';        
         ScanFor = 'pet';                                     
         ReplaceWith = 'wallaby';                              
         Pos = %scan(ScanFor: OriginalString);                
         ShiftRepl = %replace(ReplaceWith: OriginalString:    
                                Pos: %len(ScanFor));          
         FixedRepl = %replace(ReplaceWith: OriginalString:
                                Pos: %len(ReplaceWith));      
         FixedRepl = %replace(ReplaceWith: OriginalString: Pos); 
         *inlr = *on;                                         
         return;
    

    The shifted replacement is assigned to the ShiftRepl variable. The unshifted replacement is assigned to FixedRepl. (Both assignments to FixedRepl are identical in function.) Notice what happens.

                   ....5...10...15...20...25...30...35...40
    OriginalString My pet's name is Harold.
    FixedRepl      My wallabyame is Harold.
    ShiftRepl      My wallaby's name is Harold.
    

    I think shifting is the way to go in this case, don’t you?

    I also think shifting is the way to go in this case if the replacement string is shorter than the original. Here I replace pet with ma.

                   ....5...10...15...20...25...30...35...40
    FixedRepl      My mat's name is Harold.
    ShiftRepl      My ma's name is Harold.
    

    Of course, one could argue that both versions are invalid, since I have neither a mat nor a mother named Harold.

    Notice I have used variable-length strings in this example. If you use fixed-length variables, consider using the %TRIMR function in order to ignore trailing blanks, as I illustrate in the following revised example.

    D OriginalString  s             64a                                 
    D FixedRepl       s                   like(OriginalString)          
    D ShiftRepl       s                   like(OriginalString)          
    D ScanFor         s             20a                                 
    D ReplaceWith     s             60a                                 
    D Pos             s              5i 0                               
     /free                                                              
         OriginalString = 'My pet''s name is Harold.';                  
         ScanFor = 'pet';                                               
         ReplaceWith = 'buzzard'; 
         Pos = %scan(%trimr(ScanFor): OriginalString);                  
         ShiftRepl = %replace(%trimr(ReplaceWith): OriginalString:      
                                Pos: %len(%trimr(ScanFor)));            
         FixedRepl = %replace(%trimr(ReplaceWith): OriginalString:      
                                Pos: %len(%trimr(ReplaceWith)));        
         FixedRepl = %replace(%trimr(ReplaceWith): OriginalString: Pos);
         *inlr = *on;                                                   
         return;                                                        
    



                         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
    New Generation Software

    Do you work on IBM i? Do you need help explaining IBM’s strategy, navigating your company’s upgrade options, or learning about IBM i releases and TRFs? Would you benefit from current programming and application development tips and links to educational resources?

    NGS has created an IBM i resources page just for you. Don’t search the web and hope to find answers to your IBM i questions.

    Visit https://ngsi.news/ilinks

    We also encourage you to visit www.ngsi.com to learn about the latest release of NGS-IQ, our IBM i query, reporting, and analytics software.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    New Generation Software:  Leading provider of iSeries BI and financial management software
    Vision Solutions:  The first new HA release from the newly merged Vision and iTera companies
    LASERTEC USA:  Fully integrate MICR check printing with your existing application

    IT Jungle Store Top Book Picks

    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database Guide: List Price, $59.00
    The iSeries Pocket Developers' Guide: List Price, $59.00
    The iSeries Pocket SQL Guide: List Price, $59.00
    The iSeries Pocket Query Guide: List Price, $49.00
    The iSeries Pocket WebFacing Primer: List Price, $39.00
    Migrating to WebSphere Express for iSeries: List Price, $49.00
    iSeries Express Web Implementer's Guide: List Price, $59.00
    Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    WebFacing Application Design and Development Guide: List Price, $55.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    The All-Everything Machine: List Price, $29.95
    Chip Wars: List Price, $29.95

    Help/Systems Issues Another Update for Robot/SCHEDULE The i5 515 and 525: IBM’s Competitive Analysis

    Leave a Reply Cancel reply

Volume 7, Number 16 -- April 25, 2007
THIS ISSUE SPONSORED BY:

WorksRight Software
Help/Systems
Guild Companies

Table of Contents

  • What Can I Select When I Group?
  • To Shift or Not to Shift: That Is in the Fourth Parameter
  • Admin Alert: Dealing with i5 Critical Storage Errors,
  • The Long and Short of Setting Up Level 40 Security

Content archive

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

Recent Posts

  • Stacking Up Power10 And Power11 Systems Price/Performance
  • Where Infor Is Headed With Its ERPs For IBM i
  • Rocket Delivers More DevOps Capabilities For IBM i
  • A Few More Power Systems Announcements Before Year End
  • IBM i PTF Guide, Volume 27, Number 46
  • Bang For The Buck On Entry Power10 And Power11 Machines
  • A Hardware Refresh Is The Perfect Time To Re-Evaluate Your HA/DR Strategy
  • Fresche Taps AI For New RPG-To-Java Conversion Tool
  • Gartner Raises 2025 IT Spending Forecast, Puts Out 2026 Prediction
  • IBM i PTF Guide, Volume 27, Number 45

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