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

    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

    • Public Preview For Watson Code Assistant for i Available Soon
    • COMMON Youth Movement Continues at POWERUp 2025
    • IBM Preserves Memory Investments Across Power10 And Power11
    • Eradani Uses AI For New EDI And API Service
    • Picking Apart IBM’s $150 Billion In US Manufacturing And R&D
    • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
    • Fresche Overhauls X-Analysis With Web UI, AI Smarts
    • Is It Time To Add The Rust Programming Language To IBM i?
    • Is IBM Going To Raise Prices On Power10 Expert Care?
    • IBM i PTF Guide, Volume 27, Number 20

    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