September 9

2 comments

How to test for RPG numeric values in alphameric fields?

By NickLitten

September 9, 2013

check, FREE, ILE, monitor, RPG, RPGLE, snippet, testn

IBM RPG is a lovely language.

RPGLE (as the latest version is known) has got some heritage. Some might say its an old language, but I say it’s kept young by its constantly evolving syntax! Testing numeric values has evolved over years, lets look at two examples.

The current version of RPGLE (IBM i V7R1 at time of writing this) has free form code, multitudes of built in functions and handles big system database crunching with as much ease as it handles little web page servlets. RPG’s evolved over 20+ years from an archaic and mystical thing bound by the rules of something called ‘the logic cycle’ into the modern power language for all IBM i Systems everywhere.

Personally, I find a few of the older functions still a bit annoying and wish there was a nicer way of handling them.

In this case, it’s time to convict RPGLE’s ability of checking data variables to see if they contain numeric or alphanumeric variables:

Your Honor – I present the defendant: TESTN

TEST(N) Is just a little bit smelly when it comes to really/truly finding out if a field is numeric or not: Try to add two values together and you must remember that ‘ 1’ is not the same as ‘___1’ or ‘0001’. So, we need to CHECK that a value is truly numeric when importing from a alpha field into a numeric field. Otherwise its… put the value in a numeric field and try to do something ‘numericish’ with it and *boom*

The other problem is that at time of writing – their is no RPG /FREE version of the TESTN built in function. For shame 🙁

Luckily we have many many ways of checking for numerics.

My two top favorites are:

The Elegant Method testing RPG numeric values in alphameric fields

You can use this to test numeric w/o indicators.

Rpg numeric values in alphameric
dcl-s Numbers char(10) Inz('0123456789') If %check(Numbers:My_Field) > 0; Error = *On; // it has non-numerics in it else; Error = *off; // everything is numeric #huzzah! EndIf;

This speaks for itself if you understand RPG.

If you dont understand RPG, then what are you doing wasting time reading this technical blurb? huh? Get on Facebook and starting clicking LIKE on random images.

Another way – The Lazy Man Method

OK – so lets admit that this is possibly the laziest technique ever.

MONITOR
EVAL WorkField = %dec(Anyfield:9: )
ON-ERROR
...... do whatever
ENDMON

Basically we are not bothering to check the field to see if its numeric but simply wrapping an error handler around it, so that if it bombs we ignore the *bomb* and do something else to fix the data. Lazy – Yes. Stupid – No!

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

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

    >