• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Spacing between Concatenated Strings

    December 13, 2002 Timothy Prickett Morgan

    Dear Readers:

    A neat feature of RPG’s CAT op code that I have used from time to time is its ability to insert a specified number of blanks between two concatenated values. The number of blanks is specified in Factor 2, following a string and a colon (:). The new way to concatenate in RPG is by using the plus sign (+) operator, but the plus sign doesn’t support the specified number of blanks option. One reader has developed a solution, and today I pass it along to you.

    — Ted

    Hey, Ted:

    Sometimes when I’m concatenating strings together using the various %TRIM functions, I want to put one or more spaces between two values in the same field, which is a simple task:

    eval name = %trimr(lastname) + ‘  ‘ + firstname.

    That nice little space, enclosed in single quotes (‘), is the point I illustrate. There are times when I want to use one line of code to do a concatenation where I might have one space one time and then two, three, or even more spaces the next time. Sometimes I don’t want spaces.

    I could code four or five different lines that are all the same, except for the number of spaces between the single quotes, but I prefer to have one line of code where I replace the single-quoted space constants with a variable length field. I can set the length of the spacer field to be equal to the number of spaces I want between the two values each time I run it. And here’s the coolest feature: If I set the length of the field to zero, then no spaces are inserted.

    In the following code, I run an EVAL statement three times, changing the length of the variable-length field SPACER, each time.

    D C1              c                   'ABC'
    D C2              C                   'DEF'
    D Spacer          s             10    varying
    D X               s              1  0 
    D Test            s             20 
       
    C     0             DO        2             X 
    C                   EVAL      %Len(Spacer) = X
    C                   EVAL      Test = C1 + Spacer + C2     
    C                   DSPLY                   Test 
    C                   ENDDO 
    

    The %LEN function sets the length of the SPACER field to zero, one or two bytes long respectively during the three tours through the loop. The results of running this program are below:

    DSPLY  ABCDEF
    *N
    DSPLY  ABC DEF
    *N
    DSPLY  ABC  DEF
    

    — John Chadwick, Project Leader

    DMC Consulting, Inc.

    johnl.chadwick@dmcconsulting.com

    Thanks for sharing this idea with us, John. This technique would be even more useful when inserting large numbers of blanks between two character strings.

    The only other idea I have along these lines comes from Visual Basic (VB), a language with which I spend a lot of time these days. VB has a SPACE function, which returns a string of a specified number of spaces. The following VB code inserts 57 spaces between two string values.

    Dim c1 As String
    Dim c2 As String
    Dim test As String
    
    c1 = "ABC"
    c2 = "DEF"
    
    test = c1 & Space(57) & c2
    

    An equivalent RPG function can be written. In the following example, SPACER is a function that returns up to 4096 blanks. The number of blanks to be inserted may be passed as a literal or variable.

    H dftactgrp(*no)
      
    D Spacer          pr          4096    varying 
    D  NbrOfBlanks                  10u 0 value
      
    D C1              c                   'ABC'
    D C2              C                   'DEF'
    D X               s              3  0 
    D Test            s            128 
       
    C                   EVAL      X = 28
    C                   EVAL      Test = C1 + Spacer(X) + C2 
        
    C                   EVAL      Test = C1 + Spacer(98) + C2
          
    C                   EVAL      *INLR = *on
        
    P Spacer          b
    D Spacer          pi          4096    varying
    D  NbrOfBlanks                  10u 0 value
    D BlankVar        s           4096    varying
    C                   EVAL      %Len(BlankVar) = NbrOfBlanks
    C                   RETURN    BlankVar
    P                 e
    

    But I think I like your solution better.

    — Ted

    Sponsored By
    inFORM DECISIONS

    ELIMINATE THE COSTS OF PRE-PRINTED FORMS, LABOR AND POSTAGE WITH
    inFORM Decisions iDocs™ ‘Suite’

    iSeries based e-Forms, e-Checks, e-Mail, e-FAX, and Document Retrieval from the Web are available as individual modules or as a complete e-Document processing ‘Suite’.

    Click to Download the Complete Suite or Individual Modules today
    www.inFORMDecisions.com

    or call (800) 858-5544

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: mgo_rc, Volume 2, Number 93 -- December 13, 2002

    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

    Readers Prefer ISDB Your System/36 Information is Very Helpful

    Leave a Reply Cancel reply

MGO Volume: 2 Issue: 93

This Issue Sponsored By

    Table of Contents

    • Reader Feedback and Insights: RPG Compiler Directives and V4
    • Ensuring Valid XML Content
    • Spacing between Concatenated Strings

    Content archive

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

    Recent Posts

    • With Power11, Power Systems “Go To Eleven”
    • With Subscription Price, IBM i P20 And P30 Tiers Get Bigger Bundles
    • Izzi Buys CNX, Eyes Valence Port To System Z
    • IBM i Shops “Attacking” Security Concerns, Study Shows
    • IBM i PTF Guide, Volume 27, Number 26
    • 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

    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