Use RPG String Manipulation Built-In Functions to Remove Spaces PDF Print E-mail
Programming - RPG
Written by Thomas Snyder   
Monday, 31 August 2009 09:49

Support MC Press - Visit Our Sponsors

 

Forums Sponsor

POPULAR FORUMS

Forums

 

 

Search Sponsor

 

Search

Create a useful procedure to remove white space from strings in RPG.

 

In a previous article, I discussed how to use string manipulation built-in functions (BIFs) to process external files. I explained %TRIM, %TRIML, %TRIMR, %SUBST, %XLATE, %CHECK, and %CHECKR. But it was brought to my attention that I failed to mention the crucial string manipulation %SCAN BIF when an MC Press Online reader named Donna G. asked me, "Do you have any suggestions on how to strip out the blanks that are in the middle of data?" 

 

She wants to take information that is coming from an EDI 850 PO document as this…


PID*F****BOX – GENIE                   20 X 14 X 14µ

…and convert it to an XML document to look like this:

<Comments>BOX - GENIE 20 X 14 X 14</Comments>

We can remove the junk characters by implementing the BIFs mentioned in the previous article. And creating the XML was not an issue for Donna. So let's focus on removing the spaces by creating a reusable procedure to implement the %SCAN BIF:

 

%SCAN(search argument : source string { : start})--

 

This function returns an unsigned integer that indicates the first position where the search argument is found within the source string. If the search argument is not found in the source string, then the returning value will be zero. The start parameter is optional and indicates the position in the source string where the scanning will begin. If the start parameter is not specified, it will default to start the scan at the beginning of the source string.

The squeezeString Procedure

To easily support the need to remove spaces from the middle of the data, we will create a procedure that uses the %SCAN BIF to find the spaces within the string.

 

When a space has been found, we will do two things:

  • Split the string at the point that the space was found using a combination of the %TRIM and %SUBST BIFs to trim both the front and back of the string to remove the surrounding spaces.
  • Drop a single space back into the string to ensure that there is only one space.

 

Then it's just a matter of lather, rinse, and repeat until we get to the end of the string.

 

Once the end of the string has been reached, we add the remaining characters from the final space search, and we're done.

 

      *-----------------------------------------------------------------

 

      * squeezeString - Squeezes out multiple spaces

 

      *-----------------------------------------------------------------

 

     P squeezeString...

 

     P                 B                   EXPORT

 

     D squeezeString...

 

     D                 PI         65535A   varying

 

     D  argInBytes                65535A   const varying

 

     D  posi           S              5S 0

 

     D  inBytes        S          65535A   varying

 

     D  outBytes       S          65535A   varying

 

     D********************************************************************

 

      /free

 

       inBytes = %trim(argInBytes);

 

       outBytes = *BLANKS;

 

       posi = %scan(' ':inBytes);

 

       dow posi > 0;

 

         outBytes = %trim(outBytes) + ' '

 

                  + %subst(inBytes:1:posi);

 

         inBytes = %trim(%subst(inBytes:posi));

 

         if (inBytes = *BLANKS);

 

           leave;

 

         else;

 

         endif;

 

         posi = %scan(' ':inBytes);

 

       enddo;

 

       if (inBytes = *BLANKS);

 

       else;

 

         outBytes = %trim(outBytes) + ' '

 

                  + %trim(inBytes);

 

       endif;

 

       return %trim(outBytes);

 

      /end-free

 

     P                 E

 

If you would like to remove all of the spaces, modify the procedure to not put the single space back into the outBytes when it is being built.

Using the squeezeString Procedure

I am not familiar with EDI 850 PO documents, so I am unsure of the patterns to identify junk. I will just trim off the first nine characters using the %SUBST BIF for this example. But, if there are patterns, you could easily implement additional logic to handle that using a combination of the string manipulation BIFS from this article and from the previous article.

 

 

     D inputBytes      S            100A

 

     D outputBytes     S            100A

 

     D displayBytes    S             52A

 

     D squeezeString...

 

     D                 PR         65535A   varying

 

     D   argInString              65535A   const varying

 

     C*

 

      /free

 

       inputBytes = 'PID*F****BOX - GENIE'

 

                  + '                   20 X 14 X 14µ';

 

       outputBytes = %subst(inputBytes:10);

 

       outputBytes = squeezeString(outputBytes);

 

       displayBytes = 'squeezeString('

 

                    + %trim(outputBytes)

 

                    + ')';

 

       DSPLY displayBytes;

 

       *inlr = *ON;

 

      /end-free

 

The Output

After running the program, you will see this output:

 

DSPLY  squeezeString(BOX - GENIE 20 X 14 X 14µ)    

 

When the strings are displayed, they are surrounded by parentheses to identify any leading and trailing blanks.

Download the Code

You can download the code used in this article--as well as the fixed-format version--by clicking here.

 

Thanks for the great question, Donna! I hope the code works well for you and everyone else who needs these capabilities.


Thomas Snyder
About the Author:

Tom Snyder has a diverse spectrum of programming experience encompassing IBM technologies, open-source, Apple, and Microsoft and utilizing these technologies with applications on the server, on the web, or on mobile devices.

 

Tom has over 20 years experience as a software developer in various environments, primarily in RPG, Java, C#, and PHP and holds certifications in Java from Sun and PHP from Zend. Prior to software development, Tom worked as a Hardware Engineer at Intel and is a proud United States Naval Veteran Submariner who served aboard the USS Whale SSN638 submarine.

 

Tom is the best-selling author of Advanced Integrated RPG, which covers the latest programming techniques for RPG ILE and Java to utilize open-source technologies.

 

Originally from and currently residing in Scranton, Pennsylvania. Tom is currently involved in a Mobile Application Start-up company named JoltRabbit LLC.

 

 


MC Press books written by Thomas Snyder available now on the MC Press Bookstore.

 

Advanced, Integrated RPG Advanced, Integrated RPG

This book shows you how to take advantage of the latest technologies from within existing RPG applications.

List Price $79.95
Now On Sale
 

 

Read More >>
Last Updated on Wednesday, 02 September 2009 07:21
 
User Rating: / 3
PoorBest 
   MC-STORE.COM