• 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
    OCEAN User Group

    OCEAN TechCon25 Online

    It’s an Exciting Time for IBM i !

    July 16 & 17, 2025 – ONLINE

    Two virtual days of learning, presented by an outstanding group of IBM’ers and IBM Champions, featuring leading-edge topics.

    FREE for OCEAN members!

    Register NOW!

    Annual (12-month) Individual OCEAN Memberships are $80 and a Corporate Membership is $250. A Corporate Membership would allow your entire company to have full access to the OCEAN website & video library and to attend OCEAN events at member rates. Act now because rates are increasing on August 1, 2025.

    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

  • With Power11, Power Systems “Go To Eleven”
  • With Subscription Price, IBM i P20 And P30 Tiers Get Bigger Bundles
  • Izzi Buys CNX, Eyes Valence Port To System Z
  • IBM i Shops “Attacking” Security Concerns, Study Shows
  • IBM i PTF Guide, Volume 27, Number 26
  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25

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