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

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    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

  • 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