• 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
    Raz-Lee Security

    Raz-Lee Security is the leader in security and compliance solutions that guard business-critical information on IBM i servers. We are committed to providing the best and most comprehensive solutions for compliance, auditing, and protection from threats and ransomware. We have developed cutting-edge solutions that have revolutionized analysis and fortification of IBM i servers.

    Raz-Lee’s flagship iSecurity suite of products is comprised of solutions that help your company safeguard and monitor valuable information assets against intrusions. Our state-of-the-art products protect your files and databases from both theft and extortion attacks. Our technology provides visibility into how users access data and applications, and uses sophisticated user tracking and classification to detect and block cyberattacks, unauthorized users and malicious insiders.

    With over 35 years of exclusive IBM i security focus, Raz-Lee has achieved outstanding development capabilities and expertise. We work hard to help your company achieve the highest security and regulatory compliance.

    Key Products:

    • AUDIT
    • FIREWALL
    • ANTIVIRUS
    • ANTI-RANSOMWARE
    • MULTI-FACTOR AUTHENTICATION
    • AP-JOURNAL
    • DB-GATE
    • FILESCOPE
    • COMPLIANCE MANAGER
    • FIELD ENCRYPTION

    Learn about iSecurity Products at https://www.razlee.com/isecurity-products/

    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

  • The IBM i Power10 Upgrade Cycle Forecast Looks Favorable
  • White Hats Completely Dismantle Menu-Based Security
  • Cloud Software To Drive Enterprise Application Growth
  • How Do You Stay In Touch With The IBM i Community?
  • IBM i PTF Guide, Volume 25, Number 6
  • Security Still Top Concern, IBM i Marketplace Study Says
  • Bob Langieri Shares IBM i Career Trends Outlook for 2023
  • Kisco Brings Native SMS Messaging to IBM i
  • Four Hundred Monitor, February 1
  • 2023 IBM i Predictions, Part 4

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 © 2022 IT Jungle

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.