• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Powerful Stuff: Creating Commands with Variable-Length Parameters!

    November 29, 2006 Ted Holt

    The code associated with this article is available for download.

    If you like to solve the same problems over and over, this tip is not for you. As far as I’m concerned, that sort of thing is for the birds. I prefer to solve a problem once and move on to other challenges. If you agree, then I have a good technique to share with you.

    One very good way to solve a problem once–and only once–is by writing your own CL commands. After all, you don’t write an RPG (COBOL, whatever HLL . . .) program when you need to make a copy of a file, do you? I suspect you use the Copy File (CPYF) command.

    One of the greatest features of CL, as I see it, is the ability to create your own commands. This means you can add new CL commands to carry out tasks that IBM has not seen fit to address. If you’ve never written a command, then I encourage you to learn to do so. If you’re already an expert at writing commands, then don’t stop reading yet, in case you’re not aware of what I’m about to share with you.

    Let’s get back to the issue of solving problems. In his article, Editing Numbers in CL, Cletus the Codeslinger pointed out a problem that appears to me to be worth solving once–that of suppressing leading zeros in order to make a number more easily read.

    A zero-suppress command would need a parameter for the unedited value. The command-processing program could update that parameter, but I would prefer to pass the edited value back through another parameter, as the Convert Date (CVTDAT) command does. How long should those parameters be? Here’s where today’s tip comes in.

    Why not allow the parameters to be of any reasonable length? Using variable-length parameters would allow you to pass variables of whatever sizes you happened to be working with at the time.

    Here’s the source code for a zero-suppression command, ZEROSUP.

    /* ============================================*/ 
    /* ZEROSUP -- Zero suppress a number           */ 
    /* ============================================*/ 
    /*  To create:                                 */ 
    /*                                             */ 
    /*     CRTCMD CMD(xxx/ZEROSUP)                 */ 
    /*            PGM(*LIBL/ZEROSUP1)              */ 
    /*            SRCFILE(xxx/QCMDSRC)             */ 
    /*            SRCMBR(ZEROSUP)                  */ 
    /*            ALLOW(*BMOD *BPGM *IMOD *IPGM)   */ 
    /*                                             */ 
    /* =========================================== */ 
                                                
    CMD   PROMPT('Zero suppress a number')      
                                                
    PARM  KWD(VALUE) TYPE(*CHAR) LEN(32) MIN(1) +     
            EXPR(*YES) VARY(*YES *INT2) PROMPT('Value') 
    PARM  KWD(TOVAR) TYPE(*CHAR) LEN(1) RTNVAL(*YES) +
            MIN(1) VARY(*YES *INT2) +                 
            PROMPT('CL var for converted data')       
    PARM  KWD(ZEROBAL) TYPE(*LGL) LEN(1) RSTD(*YES) + 
            DFT(*YES) SPCVAL((*YES '1') (*NO '0')) +  
            EXPR(*YES) PROMPT('Show a zero balance?')
    

    Notice the VARY keyword on the first two parameter definitions. The *YES value indicates that the parameter can vary in length. The *INT2 value indicates that the parameter’s length will be passed to the command-processing program in a two-byte prefix to the parameter value. That is, a four-byte value will be passed to the CPP in six bytes.

    It is the CPP’s job to determine how many bytes were passed for each parameter and to process values of those lengths. That is, if you pass a five-byte character value to the command, the CPP must not read more than five bytes. If you receive the zero-suppressed number into a 12-byte variable, the CPP must not modify any more than 12 bytes.

    The CPP must look at the first two bytes of a variable-length parameter to determine the parameter’s current length. The parameter’s value will then be extracted beginning with byte three of the parameter. In the following code fragment, from the CPP ZEROSUP1, the programmer determines how many bytes were passed in through parameter &inFromVal. (My experience is that at least one character is always passed, even if that character is a blank.)

    pgm parm(&inFromVal &ouToVar &inZeroBal)
    
      dcl &inFromVal *char 34 /* the value to be zero-suppressed       */
      dcl &ouToVar   *char 34 /* the receiving variable                */
      dcl &inZeroBal *lgl   1 /* 1=return one zero for a zero value    */
                              /* 0=return blanks for a zero value      */
    
    /* How big is the input value (the value with leading zeros)? */
      chgvar  &ValueSize    %bin(&inFromVal 1 2)
      if (&ValueSize *gt 0) do
         chgvar  &WorkValue  %sst(&inFromVal 3 &ValueSize)
      enddo
      else do
         chgvar  &WorkValue  ' '
      enddo
    

    Once you have learned to use variable-length parameters, you will be able to think of many ways to use them to develop flexible code. Here are a few ideas to get the creative energies in motion.

    • a command that suppresses whichever leading character you name
    • a command to reverse a string
    • a command to fill a string by repeating a character string
    • a command to return the length of a string without trailing blanks
    • a command to insert editing characters
    • a command to right-adjust a string
    • a command to center a string
    • a command to assign a value to a string only if that string is blank

    RELATED STORIES

    Editing Numbers in CL

    Variable-Length Character Data

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    DRV Tech

    Get More Out of Your IBM i

    With soaring costs, operational data is more critical than ever. IBM shops need faster, easier ways to distribute IBM applications-based data to users more efficiently, no matter where they are.

    The Problem:

    For Users, IBM Data Can Be Difficult to Get To

    IBM Applications generate reports as spooled files, originally designed to be printed. Often those reports are packed together with so much data it makes them difficult to read. Add to that hardcopy is a pain to distribute. User-friendly formats like Excel and PDF are better, offering sorting, searching, and easy portability but getting IBM reports into these formats can be tricky without the right tools.

    The Solution:

    IBM i Reports can easily be converted to easy to read and share formats like Excel and PDF and Delivered by Email

    Converting IBM i, iSeries, and AS400 reports into Excel and PDF is now a lot easier with SpoolFlex software by DRV Tech.  If you or your users are still doing this manually, think how much time is wasted dragging and reformatting to make a report readable. How much time would be saved if they were automatically formatted correctly and delivered to one or multiple recipients.

    SpoolFlex converts spooled files to Excel and PDF, automatically emailing them, and saving copies to network shared folders. SpoolFlex converts complex reports to Excel, removing unwanted headers, splitting large reports out for individual recipients, and delivering to users whether they are at the office or working from home.

    Watch our 2-minute video and see DRV’s powerful SpoolFlex software can solve your file conversion challenges.

    Watch Video

    DRV Tech

    www.drvtech.com

    866.378.3366

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Integrated Print Solutions:  Print AFP/IPDS documents to any network printer
    COMMON:  Join us at the Annual 2007 Conference & Expo, April 29 - May 3, in Anaheim, California
    Canvas Systems:  We build and deliver custom iSeries rental solutions

    Seagull Software Shows Growth in First Half of Fiscal 2007 The System iWant, 2007 Edition

    Leave a Reply Cancel reply

Volume 6, Number 42 -- November 29, 2006
THIS ISSUE SPONSORED BY:

SEQUEL
IBS
Centerfield Technology

Table of Contents

  • Powerful Stuff: Creating Commands with Variable-Length Parameters!
  • Harnessing Your ODBC Users with Exit Programs
  • Using Different ODBC DSN Types for i5 Access

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