• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Advanced Pattern Matching in RPG

    January 12, 2005 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.

    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

    Help/Systems’ Scheduler Now Automates EnterpriseOne Jobs Testing At iSeries Shops Not Up to Snuff, Original Finds

    Leave a Reply Cancel reply

Volume 5, Number 2 -- January 12, 2005
THIS ISSUE
SPONSORED BY:

ProData Computer Svcs
Patrick Townsend & Associates
Guild Companies

Table of Contents

  • More on RPG Encapsulation
  • Advanced Pattern Matching in RPG
  • The Dangers of Temporarily Changing User Profiles

Content archive

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

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

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