Newsletters   Subscriptions  Forums  Store   Career  Media Kit  About Us  Contact  Search   Home 
fhg
Volume 5, Number 2 -- January 12, 2005

Advanced Pattern Matching in RPG

by Michael Sansoterra


The code for this article is available for download.


SQL has many great features, including the LIKE predicate. LIKE's power is manifest in its ability to have multiple wild cards in a search pattern. Unfortunately, RPG's closest analog to LIKE is the %SCAN built-in function, which is limited to searching a string for a single substring.

Even though %SCAN's power is limited, a little extra code can be used to simulate LIKE. The code in program LikeR.RPGLE contains subprocedure Like. The parameters for the Like subprocedure are:

Like(search string:pattern[:wildcard])

An indicator variable is returned by Like to indicate whether the string contains the requested pattern. The wildcard parameter is optional; if unspecified, the percent sign (%) will be assumed.

For the purpose of illustrating the Like subprocedure's power, program LikeR.RPGLE can be called from the command line with two or three arguments (search, pattern, and the optional wildcard). The Dsply opcode is used to indicate with a 1 or a 0 as to whether the pattern matched.


Samples


The trick to this type of pattern matching with %SCAN is to break the pattern into its components and search for each component sequentially.


Search String Pattern Wildcard Match
ABCDEFGHIJKLMNOPQRS %G%Q%S% % Yes
ABCDEFGHIJKLMNOPQRS A%P % No
The % is a test% *is*test* * Yes
RPG is a great language is a great% % No
A1064 A1064 % Yes

For comparative purposes, here are a few code examples of how Like compares to %Scan:


Example 1


Search='The quick brown fox';

// Search for pattern assuming the default wildcard (%)
If Like(Search:'%quick%fox%');
    Dsply 'LIKE %quick%fox%' '' *InLR;
EndIf;
//
// Here is the equivalent use of the scan op-code
//
If %Scan('quick':Search)>*Zero
And %Scan('fox':Search)>%Scan('quick':Search);
    Dsply '%SCAN %quick%fox%' '' *InLR;
EndIf;

Example 2


Search='The quick brown fox';

// Test if pattern begins with "The" and ends with "fox"
If Like(Search:'The%fox');
    Dsply 'LIKE The%fox' '' *InLR;
EndIf;

// The equivalent RPG code is
// (actually "bullet proof" code would be much longer because
//  it would test acceptable string lengths, start pos, etc.)
If %Subst(Search:1:3)='The'
And %Subst(Search:%Len(Search)-2:3)='fox';
    Dsply 'SCAN The%fox' '' *InLR;
EndIf;

As you can see, the Like subprocedure provides shorter code and offers more flexibility.


Here are a few additional notes on the LIKE subprocedure:

  • It does not recognize single character substitution (the underscore _ character).

  • The pattern matching is case-sensitive.

  • Trailing spaces are significant, so use the %TrimR function to remove them when necessary.

This subprocedure can be used to provide a subset feature for subfile programs or reports that don't use embedded SQL. Creating simple subprocedures such as LIKE can quickly pay off by adding powerful functionality to any number of programs.


Michael Sansoterra is a programmer/analyst for i3 Business Solutions, an IT services firm based in Grand Rapids, Michigan. Click here to contact Michael Sansoterra by e-mail.

Sponsored By
PATRICK TOWNSEND & ASSOCIATES

Alliance AuthExpress
Authorize Visa, MasterCard, Discover and American Express
on your AS/400

  • Native AS/400 solution with secure authorization
  • Interactive and batch authorization
  • Web application interface for eCommerce
  • Free 30-day trial. Fully functional software - Not a demo.

Find out more at www.patownsend.com
Visit us for 30-day trial
Deploy. Run. Manage. Succeed.

Alliance AuthExpress provides a reliable, scalable, and manageable platform for the authorization of credit card, value card, gift card, and check transactions. Your organization can start with a small volume of transactions and grow to a very large number of transactions without changing your authorization software. Alliance supports a variety of authorization networks including Vital, Visa (Visa Access Point and Direct Exchange), First Data, Alliance Data Systems, and others. Interfaces to the system include TCP sockets, data queues, and secure FTP. You can directly connect Point of Sale systems or other computing platforms to Alliance for authorization services.

Alliance AuthExpress provides three entry points for the small, medium, and large enterprise. Alliance AuthExpress Internet Edition provides the starting point for the small company. Alliance AuthExpress Internet Edition includes a license to Alliance Application Monitor for system monitoring, Alliance BankLink/400 for dial communications and standard support is also included.

Alliance AuthExpress SMB Edition provides a starting point for the medium sized business. The SMB server supports up to 2,500 transactions per day averaged over a calendar month. Alliance AuthExpress SMB Edition includes a license to Alliance Application Monitor for system monitoring, Alliance BankLink/400 for dial communications, Alliance FTP Manager for secure SSL FTP transmission to your bank, and standard support is also included.

Alliance AuthExpress Enterprise Edition provides for an unlimited number of transactions per day and includes a license to Alliance Application Monitor for system monitoring, Alliance BankLink/400 for dial communications, Alliance FTP Manager for secure SSL FTP transmission to your bank, Alliance PGP Option for strong encryption, and 24/7 support is also included. The Enterprise Edition is designed for the large retail or commercial enterprise with a high volume of transactions.

Patrick Townsend & Associates has joined with the Boaz Group to provide exclusive payment processing solutions to the Motorcycle Industry Council (MIC) members and their customers through the MIC BankCard Program. Members can now access state-of-the-art payment processing solutions through their IBM AS/400 and iSeries systems at special low bundled rates. The Alliance AuthExpress MIC Option provides additional services and software for MIC members.

Find out more at www.patownsend.com
Visit us for 30-day trial
Deploy. Run. Manage. Succeed.


Technical Editors: Howard Arner, Joe Hertvik, Ted Holt,
Shannon O'Donnell, Kevin Vandever
Managing Editor: Shannon Pastore
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, 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.


THIS ISSUE
SPONSORED BY:

ProData Computer Svcs
Patrick Townsend & Associates
Guild Companies


BACK ISSUES

TABLE OF
CONTENTS
More on RPG Encapsulation

Advanced Pattern Matching in RPG

The Dangers of Temporarily Changing User Profiles


The Four Hundred
Borman Out, Shearer In As iSeries General Manager

Q&A with Mark Shearer, the New iSeries GM

RFID Specialist Stratum Global Spins Off from LANSA

Four Hundred Stuff
Tango/04 Delivers Affordable BSM, or 'Tivoli for the Rest of Us'

eSP Creates New Product Category: 'Terminal Session Management'

ProData's New DBUnifier Creates Interactive Apps in a Jiffy

Four Hundred Monitor


Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc. (formerly Midrange Server), 50 Park Terrace East, Suite 8F, New York, NY 10034
Privacy Statement