fhg
Volume 7, Number 16 -- April 25, 2007

To Shift or Not to Shift: That Is in the Fourth Parameter

Published: April 25, 2007

by 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


Sponsored By
HELP/SYSTEMS

SEQUEL can be used for
virtually ALL business intelligence functions
on the System i, including:

                                                    · Executive Dashboards
                                                    · Graphical Query & Reporting
                                                    · Drill-Down Data Analysis
                                                    · Multi-Platform Database Support
                                                    · E-Mail Report and File Distribution
                                                    · Secure Web Access

SEQUEL is the single solution for all
your business intelligence needs.

www.helpsystems.com


Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

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

 

The Four Hundred
Power6: Later in 2007 Rather than Sooner?

Slowing U.S. Sales Hurt IBM's First Quarter

Reader Feedback on User-Priced System i Boxes

As I See It: Induced Labor

The Linux Beacon
FastScale Takes a Different Approach to Virtualization and Provisioning

Sun, Canonical Integrate Java, GlassFish, and NetBeans into Ubuntu

Round Two: Intel's Fortunes Rise, and AMD's Fall

Slowing U.S. Sales Hurt IBM's First Quarter

Four Hundred Stuff
PowerTech Tools Build Trust By Decreasing Authority

IBM Expects Speedier Portal Projects

BSafe Introduces Cross-Platform Auditing

CCSS Addresses SOX Requirements in QMessage Monitor

Big Iron
Slowing U.S. Sales Hurt IBM's First Quarter

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
April 21, 2007: Volume 9, Number 16

April 14, 2007: Volume 9, Number 15

April 7, 2007: Volume 9, Number 14

March 31, 2007: Volume 9, Number 13

March 24, 2007: Volume 9, Number 12

March 17, 2007: Volume 9, Number 11

The Windows Observer
'Viridian' Beta Delayed. Is Longhorn Next?

Windows Server DNS Flaw Being Exploited

Dell, IBM Push Power-Saving Servers

Marathon Makes Virtualization Fault Tolerant with v-Available

The Unix Guardian
Fujitsu, Sun Deliver Joint Sparc Enterprise Server Line

Power6: Later in 2007 Rather than Sooner?

Slowing U.S. Sales Hurt IBM's First Quarter

As I See It: Disorderly Conduct

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

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, Part 1

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
User profile for webserver instances

Specific Record Range (CL)

Select/omit error on member

Encoded Vector Index(EVI)--Throw some light

Kurt Vonnegut is dead





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement