• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Unlocking The Power Of %CONCAT And %CONCATARR In RPG

    April 14, 2025 Gregory Simmons

    When working with strings in RPG, especially in modern free-format code, the %CONCAT and %CONCATARR built-in functions (BIFs) offer efficient and readable solutions for combining strings. These functions simplify the process of concatenating multiple string values, making your code cleaner and easier to maintain. In this article, we will explore how they work and demonstrate their practical applications.

    The %CONCAT BIF allows you to combine multiple strings. It’s a more efficient and readable alternative to traditional concatenation using the + operator.

    Here’s a simple example:

    **Free
    Ctl-Opt Main(CONCAT_1) ActGrp(*Caller);
    
    Dcl-Pr pause ExtProc('sleep');
      time_to_sleep Uns(10) Value;
    End-Pr;
    
    Dcl-Proc CONCAT_1;
    
      Dcl-S firstName Char(20) Inz('John');
      Dcl-S lastName  Char(20) Inz('Doe');
      Dcl-S fullName  Char(50);
    
      fullName = %CONCAT(‘ ‘:firstName:lastName);
      SND-MSG *STATUS fullName %TARGET(*EXT);  // Output:John                Doe
      pause(3);
    
    End-Proc CONCAT_1;
    

    In this example, %Concat combines firstName, a space, and lastName into a single fullName variable. It’s clean and eliminates the need for multiple concatenation operators. Compiling and running this program will output this to the bottom of your screen and wait for 10 seconds before continuing execution:

    ‘John                Doe’
    

    On a side note, have you played with the Snd-Msg operator yet? I find that I like it better than Dsply. When I use the Snd-Msg, combined with the *Status and %Target(*EXT) parameters, it’s just nicer to have the message(s) show up under my command line instead of flashing really quick and then I have to display the joblog to see the output. Yes, you can make the Dsply operation ‘pause’ but then you have to press enter to continue processing; I just like the flow afforded by the Snd-Msg operation better. If you haven’t already, I recommend you give it a try.

    Okay, back to the previous example. The output is a bit ugly because the variables are 20 characters long and the value I initialized them to are only four and three long, respectively. There are a couple ways to make this nicer. One practical usage of this would be to combine the above example with the %Trim BIF. Like this:

      fullName = %Concat(‘ ‘:%Trim(firstName):%Trim(lastName));
      SND-MSG *STATUS fullName %TARGET(*EXT); // Output:John Doe
    

    Much nicer!

    Another, perhaps even nicer way to clean that output up is to simple change the declaration of the firstName and lastName standalone variables from Char(20) to VarChar(20). I prefer working with VarChar, but both tactics will have the desired result.

    The %CONCATARR BIF is particularly useful when you need to concatenate elements from an array. It simplifies scenarios where you would otherwise loop through an array to combine values.

    **Free
    Ctl-Opt Main(CONCAT_2) Ctl-Opt ActGrp(*Caller);
    
    Dcl-Pr pause ExtProc('sleep');
      time_to_sleep Uns(10) Value;
    End-Pr;
    
    Dcl-Proc CONCAT_2;
    
      Dcl-S names VarChar(20) Dim(4) Inz(*Blanks);
      Dcl-S nameList VarChar(100);
    
      names(1) = 'Alice';
      names(2) = 'Bob';
      names(3) = 'Charlie';
      names(4) = 'David';
      nameList = %Concatarr(', ':names);
      SND-MSG *STATUS nameList %TARGET(*EXT); 
      // Output:Alice, Bob, Charlie, David
      pause(3);
    
    End-Proc CONCAT_2;
    

    In the above example, note that for the first parameter for %CONCATARR, I passed a comma and then one blank space. This keeps the output string a nicely formatted list of names. However, as with many, if not all BIFs in RPG, that parameter could be any expression you needed; providing the result of your expression is a string.

    It is also important to note that in my example, to keep it simple, the ‘names’ array only has Dim(4) or 4 elements. This is not a practical real-world example. If, for example, I changed the array to Dim(1000), and re-ran the program, the output would be ‘Alice, Bob, Charlie, David’ followed by 996 repetitions of ‘, ‘. I can change the %CONCATARR statement to only consider the elements in the array which are not blank, using this approach:

      nameList = %ConcatArr(', ':%SubArr(names:1:%Lookup(' ':names:1)-1));
    

    This new version, with the addition of the %SUBARR and %LOOKUP BIFs, instructs the %CONCATARR BIF to only use the portion of the array which contain non-blank values. Perhaps a nicer way to solve the issue of excessive comma-space at the end of your resulting string would be to change the definition of your array:

    **Free
    Ctl-Opt Main(CONCAT_3) ActGrp(*Caller);
    
    Dcl-Pr pause ExtProc('sleep');
      time_to_sleep Uns(10) Value;
    End-Pr;
    
    Dcl-Proc CONCAT_3;
    
      Dcl-S names VarChar(20) Dim(*Auto:10);
      Dcl-S nameList VarChar(100);
    
      names(1) = 'Alice';
      names(2) = 'Bob';
      names(3) = 'Charlie';
      names(4) = 'David';
      nameList = %Concatarr(', ':names);
      SND-MSG *STATUS nameList %TARGET(*EXT); 
      //Output:Alice, Bob, Charlie, David
      pause(3);
    
    End-Proc CONCAT_3;
    

    The previous two examples have the same output, but I believe the second example reads more nicely.

    One final note – All of my examples above demonstrate using %CONCAT and %CONCATARR to combine strings together, with something to act as a spacer between the strings. But if you have a need to combine the strings together without a spacer, both BIFs support *NONE as their first parameter.

    Using %CONCAT and %CONCATARR in RPG can greatly enhance your string manipulation capabilities. They not only make your code cleaner and more readable but also reduce potential errors caused by manual concatenation.

    Until next time, happy (assisted) coding.

    Gregory Simmons is a Project Manager with PC Richard & Son. He started on the IBM i platform in 1994, graduated with a degree in Computer Information Systems in 1997 and has been working on the OS/400 and IBM i platform ever since. He has been a registered instructor with the IBM Academic Initiative since 2007, an IBM Champion and holds a COMMON Application Developer certification. When he’s not trying to figure out how to speed up legacy programs, he enjoys speaking at technical conferences, running, backpacking, hunting, and fishing.

    RELATED STORIES

    Guru: AI Pair Programming In RPG With Continue

    Guru: AI Pair Programming In RPG With GitHub Copilot

    Guru: RPG Receives Enumerator Operator

    Guru: RPG Select Operation Gets Some Sweet Upgrades

    Guru: Growing A More Productive Team With Procedure Driven RPG

    Guru: With Procedure Driven RPG, Be Precise With Options(*Exact)

    Guru: Testing URLs With HTTP_GET_VERBOSE

    Guru: Fooling Around With SQL And RPG

    Guru: Procedure Driven RPG And Adopting The Pillars Of Object-Oriented Programming

    Guru: Getting Started With The Code 4 i Extension Within VS Code

    Guru: Procedure Driven RPG Means Keeping Your Variables Local

    Guru: Procedure Driven RPG With Linear-Main Programs

    Guru: Speeding Up RPG By Reducing I/O Operations, Part 2

    Guru: Speeding Up RPG By Reducing I/O Operations, Part 1

    Guru: Watch Out For This Pitfall When Working With Integer Columns

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, FHG, Four Hundred Guru, IBM i, RPG

    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

    As I See It: Lucie, Lucie, Lucie z17 Mainframes Give IBM Time To Ramp AI-Accelerated Power11 Systems

    One thought on “Guru: Unlocking The Power Of %CONCAT And %CONCATARR In RPG”

    • Ema Tissani says:
      April 14, 2025 at 7:24 am

      Thanks for the SND-MSG hint… indeed superior to leverage a full logging / messaging system instead of a bland stderr… and the thing is that, with some techniques, we are basically imitating the worst of the open world… like having a source in a IFS is now “cool” compared to a much more structured DB based approach…… we are going backwards ;P

      Reply

    Leave a Reply Cancel reply

TFH Volume: 35 Issue: 14

This Issue Sponsored By

  • Rocket Software
  • New Generation Software
  • DRV Tech
  • WorksRight Software
  • Raz-Lee Security

Table of Contents

  • IBM i 7.6 Brings More Security Improvements Than Just MFA
  • z17 Mainframes Give IBM Time To Ramp AI-Accelerated Power11 Systems
  • Guru: Unlocking The Power Of %CONCAT And %CONCATARR In RPG
  • As I See It: Lucie, Lucie, Lucie
  • IBM i PTF Guide, Volume 27, Number 15

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