RPG example to strip out all non-numeric junk from a telephone number field in RPG ILE 

 March 17, 2017

By  NickLitten

RPG 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.         


[thrive_headline_focus title=”UPDATED MARCH 2017″ orientation=”left”]

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.




IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"cff50":{"name":"Main Accent","parent":-1},"a344d":{"name":"Accent Transparent","parent":"cff50"}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"cff50":{"val":"var(--tcb-skin-color-0)"},"a344d":{"val":"rgba(46, 138, 229, 0.85)","hsl_parent_dependency":{"h":210,"l":0.54,"s":0.78}}},"gradients":[]},"original":{"colors":{"cff50":{"val":"rgb(0, 178, 255)","hsl":{"h":198,"s":1,"l":0.5}},"a344d":{"val":"rgba(0, 178, 255, 0.85)","hsl_parent_dependency":{"h":198,"s":1,"l":0.5}}},"gradients":[]}}]}__CONFIG_colors_palette__

Get In Touch

I’m always looking for awesome input, feedback and critique!