• 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
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    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

  • Public Preview For Watson Code Assistant for i Available Soon
  • COMMON Youth Movement Continues at POWERUp 2025
  • IBM Preserves Memory Investments Across Power10 And Power11
  • Eradani Uses AI For New EDI And API Service
  • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20

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