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

    The Leader in IBM i Education!
    Need training on anything i?
    Manta is all you need.

    130 courses and competency exams on:
    · IBM i operations
    · System Management and Security
    · IBM i Programming Tools
    · Programming in RPG, COBOL, CL, Java
    · Web Development

    SQL, DB2, QueryProduct features:
    · Runs in every popular browser
    · Available 24/7/365
    · Free Student Reference Guides
    · Free Student Administration
    · Concurrent User License
    · Built-In IBM i Simulator

    You can download our 200-page catalog and take sample sessions at MantaTech.com

    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

  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18
  • Will The Turbulent Economy Downdraft IBM Systems Or Lift It?
  • How IBM Improved The Database With IBM i 7.6
  • Rocket Celebrates 35th Anniversary As Private Equity Owner Ponders Sale
  • 50 Acres And A Humanoid Robot With An AI Avatar
  • IBM i PTF Guide, Volume 27, Number 17

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