Warning: Declaration of TCB_Menu_Walker::walk($elements, $max_depth) should be compatible with Walker::walk($elements, $max_depth, ...$args) in /home/nicklit/www/www/wp-content/plugins/thrive-visual-editor/inc/classes/class-tcb-menu-walker.php on line 620

Warning: session_start(): Cannot start session when headers already sent in /home/nicklit/www/www/wp-content/plugins/userpro/includes/class-userpro.php on line 222
RPG example to strip out all non-numeric junk from a telephone number field in RPG ILE - Nick Litten is IBM-i, AS400 iSeries RPG Programmer and Nerd

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

IBM i

Mar 17

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 UPPER const('ABCDEFGHIJKLMNOPQRSTUVWXYZ?');
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);
Enddo;

dsply numbersOnly;

*inlr = *on;
return;

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

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

 

UPDATED MARCH 2017

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.

 

🙂

Follow

About the Author

IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Alpha Nerd 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 in the words of the most interesting man in the world: Stay thirsty my friend.