RPG Example to remove Letters from Telephone Number field in RPG ILE

  • Home
  • /
  • Blog
  • /
  • RPG Example to remove Letters from Telephone Number field in RPG ILE

March 17, 2017

RPG Example to remove Letters from Telephone Number field in RPG ILE

By NickLitten

March 17, 2017


RPG Example to remove Letters from Telephone Number

Just working on a project to create an American IRS 1099 Extract File. Lots of field data management rules in the file thats going to be sent to the IRS. One of them is that all telephone number fields must be numeric only left adjusted. Of course, in our Alphameric AS400 JBA System the phone number if free format and can be stored just however the user has entered. Some look like 123-456-4564 some like 123 123 1234 or (123)123-1234 or peoples names, extensions and the rest.

A little head scratching and coding and I found this neat solution… yes I know the UP, LO and SPECIAL values could be put in one value BUT I though it made it more readable and already had the UP and LO defined for some case conversion on another field.

Here’s a little code example that explains it:

Rpg to remove letters from telephone numberdcl-s telephoneNumber char(50) inz(' Cell: (123) 333-4444.');
dcl-s numbersOnly varchar(50);

// case conversion variables
dcl-c lower const('abcdefghijklmnopqrstuvwxyz?');
dcl-c gubbins const('!@#$%¢&*()_+=-¬¦{}¬¦;:,.<>?');
dcl-c nuffink const('                           ');

// strip out all alpha characters and left adjust the number
// for example - a number entered as (540)362-8521ext77
// would be returned as 540362852177
// HASHTAG a nice alternative to read each element and checking if its 1234567890
dsply telephoneNumber;

numbersOnly = telephoneNumber;
numbersOnly = %xlate(UPPER:nuffink:numbersOnly);
numbersOnly = %xlate(lower:nuffink:numbersOnly);
numbersOnly = %xlate(gubbins:nuffink:numbersOnly);

// read through the field and squish it the left
DoW %Scan(' ': %TRIMR(numbersOnly)) > *Zeros;
 numbersOnly=%Replace('':numbersOnly:%Scan(' ':%TRIMR(numbersOnly)):1);

dsply numbersOnly;

*inlr = *on;

So when this code snipper you should see something like this:

DSPLY    Cell: (123) 333-4444.   
DSPLY  1233334444                
Press Enter to continue.         


I just found this little old code snippet in my blog, after my website statistics showed me it was visited 23 times last week — Is someone doing a telephone number cleanup routine by any chance? LOL — I originally wrote this back in 2012, so it’s time for a quick RPG code upgrade and I realised that there is a much cleaner way of doing this with the %SCANREPLACE built in function.

Simply replace the Do loop with this:

numbersOnly = %triml(%scanRpl(' ':'':numbersOnly));

which says “scan for a blank ‘ ‘ and replace with a nothing ” and then trim it to the left.

Same result and easier to code and read I think.


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Join the IBM i Community for FREE Presentations, Lessons, Hints and Tips