• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Find the Length of a Character String in CL, Take Two

    December 7, 2005 Hey, Ted

    Just yesterday, I was trying to determine the length of the value of a CL variable, so your tip today using the MSGDTA trick was helpful. I was thinking that IBM had added %TRIM and %LEN to the list of CL built-in functions (BIFs) that exist in recent releases, but I guess I was imagining this. Why hasn’t IBM done this, with all the other great enhancements to CL recently? Is there a way to add your own BIFs to CL by creating your own commands?

    –Flagler

    I have long advocated the creation of commands to extend the CL language, but I find that many programmers have never created a command. So, yes, I think you have a good idea by suggesting the use of commands, even though the commands you create are not true built-in functions.

    One reader, Steve, sent in a command he uses to determine the length of character values up to 2000 bytes long. Here’s the command definition source code.

    /* Command: RTVVARLEN                     */
    /* CPP: RTVVARLENC                        */
    /* Retrieve the length of a CL variable   */
    /* without trailing blanks                */
     CMD     PROMPT('Return the length of a CL var')
     PARM    KWD(STRING) TYPE(*CHAR) LEN(2000) MIN(1) +
               EXPR(*YES) VARY(*YES) CHOICE('String +
               variable') PROMPT('String variable to check')
     PARM    KWD(LEN) TYPE(*DEC) LEN(4) RTNVAL(*YES) +
               MIN(1) PROMPT('Return variable (4,0)')
    

    Steve didn’t tell me what he calls his command, so I used the name RTVVARLEN (Retrieve Variable Length). Here’s the command to create the command.

    CRTCMD CMD(xxx/RTVVARLEN)
           PGM(*LIBL/RTVVARLENC)
           SRCFILE(xxx/QCMDSRC)
           ALLOW(*BPGM *IPGM *BREXX *IREXX)
    

    The command-processing program is short and simple.

    /* Retrieve the length of a CL variable */
    /* with trailing blanks removed */
    PGM        PARM(&STRING &LEN)
    DCL        VAR(&STRING) TYPE(*CHAR) LEN(2000)
    DCL        VAR(&LEN) TYPE(*DEC) LEN(4 0)
    CHGVAR     VAR(&LEN) VALUE(%BIN(&STRING 1 2))
    

    I called the program RTVVARLENC and implemented it as an ILE program compiled to run in the calling program’s activation group.

    CRTBNDCL PGM(xxx/RTVVARLENC)
             SRCFILE(xxx/QCLSRC)
             SRCMBR(RTVVARLENC)
             DFTACTGRP(*NO)
             ACTGRP(*CALLER)
    

    Last, here’s the program I used to test Steve’s command. I called it RTVVARLENT.

    PGM        PARM(&P1)
    DCL        VAR(&P1) TYPE(*CHAR) LEN(32)
    DCL        VAR(&LEN) TYPE(*DEC) LEN(4)
    RTVVARLEN  STRING(&P1) LEN(&LEN)
    DMPCLPGM
    

    I found it returned the correct length of anything I passed to it in the first parameter.

    I can’t answer the first question, but I am encouraged by the enhancements IBM made to the CL compiler in V5R3 and am hopeful that we will see more and better things in future releases.

    –Ted


    RELATED STORY

    Find the Length of a Character String in CL

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Computer Keyes

    Fax Directly from your IBM i

    KeyesFax is a full function automated IBM i fax system. Spooled files are burst by fax number and auto transmitted with overlays.  It combines both a send and receive facsimile processing system with a complete image package.

    The fax software will edit, send, receive, display, print, and track fax documents or images using any standard IBM i without additional expensive hardware, software or subscriptions.

    Computer Keyes has been developing Software Solutions since 1978!

    www.computerkeyes.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    ProData Goes ‘On Demand’ with DBU IBM Delivers Scaled-Down Content Manager for iSeries

    Leave a Reply Cancel reply

Volume 5, Number 45 -- December 7, 2005
THIS ISSUE
SPONSORED BY:

Advanced Systems Concepts
WorksRight Software
iTera

Table of Contents

  • Find the Length of a Character String in CL, Take Two
  • Installing WebSphere Application Server 6.0
  • Admin Alert: Backing Up i5/OS Partitions to Disk

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Tool Aims To Streamline Git Integration For Old School IBM i Devs
  • IBM To Add Full System Replication And FlashCopy To PowerHA
  • Guru: Decoding Base64 ASCII
  • The Price Tweaking Continues For Power Systems
  • IBM i PTF Guide, Volume 27, Numbers 31 And 32
  • You Can Now Get IBM Tech Support For VS Code For i
  • Price Cut On Power S1012 Mini Since Power S1112 Ain’t Coming Until 2026
  • IBM i: Pro and Con
  • As I See It: Disruption
  • IBM i PTF Guide, Volume 27, Number 30

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