You are currently on IBM Systems Media’s archival website. Click here to view our new website.

IBM i > DEVELOPER > RPG

Forum Inspiration

Ideas for trimming spaces and multiple field concatenation

Ideas for trimming spaces and multiple field concatenation

Sometimes trying to decide what we're going to write about is the hardest part of the job. One source of inspiration is the Internet forums we subscribe to. If a topic seems to have interest beyond the scope of the original question, it gets added to our list. Such is the case for today's topics, the first of which could also be titled "the many ways to skin a cat."

Trimming Spaces Within a String

The original poster was faced with the task of editing a string to replace multiple spaces within that string with a single space. When we read the question, several possibilities sprang to mind, ranging from RPG III style techniques to the latest BIFs available in V7.1.

We decided to give the various options a try and see if there were any obvious differences in perform-ance. In the end, the options displayed very little difference, and such differences were only perceptible when the operation was repeated some 50,000 times!

One thing that did quickly become apparent though was the relative comprehensibility of the different op-tions. The examples that follow utilize these basic data definitions. The field values supplies the test data from which the excessive embedded blanks are to be removed. The field data is the main one upon which the processing will operate, and the field outData will be used by our RPG III style routine as its output field.

d values     s     40a   varying
d                        inz('ABC DEF HI JKL MN X')
d data       s     40a   varying
d char       s      1a   dim(40) based(pchar)
d pchar      s       *   inz(%addr(data: *Data))

d outData    s     40a   varying inz
d spaceSet   s       n   inz(*off)

Note that the array char is simply a redefinition of the field data in array form. Also we’ve used varying length fields in all cases; the second part of this article will help explain why.

Let's begin with the RPG III style solution. The basic idea is to loop through the input field copying each character in turn to the output—unless the character represents a second or subsequent space within the field. This actual control is through use of the indicator spaceSet, which is set as soon as a space has been copied to the output string and will prevent further spaces from being added until after a non-space character is detected. The only real difference between this and what we could have actually done in RPG III is the use of a pointer and varying length fields and their characteristics.

data = %TrimR(values);
outData = '';              // Set output to null (i.e. No data)

for i = 1 to %len(data);
  if (char(i) <> *blank);
    outdata += char(i);   // Add all non-blank characters to output
    spaceSet = *Off;      // and clear the space output flag
  else;
    if not spaceSet;      // If no space has already been output ...
      spaceSet = *On;
      outdata += char(i); // Add the space to output
    endIf;
  endIf;
endFor;

Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.



Advertisement

Advertisement

2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters