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

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    Learn More

    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

  • Public Preview For Watson Code Assistant for i Available Soon
  • COMMON Youth Movement Continues at POWERUp 2025
  • IBM Preserves Memory Investments Across Power10 And Power11
  • Eradani Uses AI For New EDI And API Service
  • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20

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