• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Cut the Gordian Knot

    April 4, 2012 Hey, Ted

    I just love those BIFs that IBM added to RPG. I can’t understand why anybody would use old RPG op codes instead of the BIFs. But I’ve run into a brick wall with %SUBST. Help!

    –Vince

    Allow me to explain Vince’s situation. He was assigned to revise a program someone else had written. The program reads a list of the file names in a certain directory on their network, and has to do other processing depending on file name extension (the part of the name following the last period).

    Here’s a program similar to the one that Vince had to modify.

    D FileName        s             16a
    D Extension       s              3a
    D Process         s              3a
    
     /free
         *inlr = *on;
         Extension = %subst(FileName: %scan('.':FileName) + 1: 3);
         if Extension = 'XLS' or Extension = 'DWG';
           Process = 'Yes';
         else;
           Process = 'No';
         endif;
         return;
    

    The %SCAN function finds the location of the period in the file name and the %SUBST function extracts the three characters that follow it. The program was processing files with extensions of XLS, XLSX, and DWG.

    Vince’s job was to add a new extension, SLDDRW, to the list of extensions to process. He revised his code like this:

    D FileName        s             16a
    D Extension       s              6a
    D Process         s              3a
    D
     /free
         *inlr = *on;
         Extension = %subst(FileName: %scan('.':FileName) + 1: 6);
         if Extension = 'XLS' or Extension = 'DWG'
               or Extension = 'XSLX' or Extension = 'SLDDRW';
           Process = 'Yes';
         else;
           Process = 'No';
         endif;
         return;
    

    The program worked fine during testing, but ended abnormally in production because the period within a long file name was four characters from the end of the FileName variable. Since the substring function extracts six characters, but only three characters followed the period, the program received message RNQ0100 (Length or start position is out of range for the string operation).

    Vince was having trouble making a coherent IF statement that would take into account such things as the number of bytes after the period. ANDs and ORs are wonderful tools, but they can make for convoluted code.

    I suggested he take another approach. Instead of trying to write a complicated logical expression full of %SUBST functions and logical operators that would produce the desired result, I suggested he write a function subprocedure to isolate the extension, which would be easy to test.

    H dftactgrp(*no) actgrp(*caller)
    
    D FileName        s             16a
    D Extension       s            128a   varying
    D Process         s              3a
    
    D GetExtension    pr           128a   varying
    D   inFileName                 128a   const
    
     /free
         *inlr = *on;
         Extension = GetExtension(FileName);
         if Extension = 'XLS' or Extension = 'DWG'
               or Extension = 'XSLX' or Extension = 'SLDDRW';
           Process = 'Yes';
         else;
           Process = 'No';
         endif;
         return;
     /end-free
    * =========================================================
    P GetExtension    b
    D                 pi           128a   varying
    D   inFileName                 128a   const
     *** locals
    D DotLoc          s             10i 0
    D Dot             c                   const('.')
    D EmptyString     c                   const('')
    D Extension       s            128a   varying
    
     /free
         monitor;
            if inFileName = *blanks;
               return EmptyString;
            endif;
            DotLoc = %scan('.': InFileName);
            if DotLoc <= *zero;
               return EmptyString;
            endif;
            Extension = %trim(%subst(inFileName: DotLoc + 1));
            dow '1';
               DotLoc = %scan('.': Extension);
               if DotLoc <= *zero;
                  return Extension;
               endif;
               Extension = %trim(%subst(Extension: DotLoc + 1));
            enddo;
         on-error;
            return EmptyString;
         endmon;
         return Extension;
     /end-free
    P                 e 
    

    Vince reports that the program has worked correctly since.

    I see a lot of code like this. Programmers commonly get so caught up in the mechanics of making the computer produce a desired result that they lose sight of the bigger picture. But that doesn’t mean you have to.

    –Ted



                         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
    VISUAL LANSA 16 WEBINAR

    Trying to balance stability and agility in your IBM i environment?

    Join this webinar and explore Visual LANSA 16 – our enhanced professional low-code platform designed to help organizations running on IBM i evolve seamlessly for what’s next.

    🎙️VISUAL LANSA 16 WEBINAR

    Break Monolithic IBM i Applications and Unlock New Value

    Explore modernization without rewriting. Decouple monolithic applications and extend their value through integration with modern services, web frameworks, and cloud technologies.

    🗓️ July 10, 2025

    ⏰ 9 AM – 10 AM CDT (4 PM to 5 PM CEST)

    See the webinar schedule in your time zone

    Register to join the webinar now

    What to Expect

    • Get to know Visual LANSA 16, its core features, latest enhancements, and use cases
    • Understand how you can transition to a MACH-aligned architecture to enable faster innovation
    • Discover native REST APIs, WebView2 support, cloud-ready Azure licensing, and more to help transform and scale your IBM i applications

    Read more about V16 here.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    i Believe:  FREE community event: i Believe. May 4. Get more info or register now!
    BCD:  Stop Spinning Your Wheels. Develop new IBM i web apps very fast with WebSmart
    COMMON:  Join us at the 2012 Conference & Expo, May 6 - 9 in Anaheim, CA

    IT Jungle Store Top Book Picks

    BACK IN STOCK: Easy Steps to Internet Programming for System i: List Price, $49.95

    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The iSeries Pocket Database Guide: List Price, $59
    The iSeries Pocket SQL Guide: List Price, $59
    The iSeries Pocket WebFacing Primer: List Price, $39
    Migrating to WebSphere Express for iSeries: List Price, $49
    Getting Started with WebSphere Express for iSeries: List Price, $49
    The All-Everything Operating System: List Price, $35
    The Best Joomla! Tutorial Ever!: List Price, $19.95

    Dell ‘Wyses’ Up with Thin Client Acquisition Some Thoughts About IBM’s Next Generation Platform

    Leave a Reply Cancel reply

Volume 12, Number 8 -- April 4, 2012
THIS ISSUE SPONSORED BY:

WorksRight Software
Infor
CNX

Table of Contents

  • Index Advisor, Part 1
  • Cut the Gordian Knot
  • Admin Alert: Readers Check in on Four Simple Rules for PTFs

Content archive

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

Recent Posts

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

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