RPG

Welcome to my little corner of the Cloud

Hello, my name is Nick Litten and I'm a technology addict, nerd, geek and Propeller Head!

In case your wondering, I'm an IT Consultant with a technology addiction - show me anything from a smartphone to a touch-screen fridge and I immediately get all jittery. Playing with Drupal on this website is just one way for me to let off steam. Nick Litten Dot Com is a mixture of blog posts that can be serious, playful and 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.

left adjust alpha into numeric using RPG language

lovely old AS400s on the road side

So, I was writing a little code snippet for a client after being told that all warehouse codes must be treated as 3 numeric character integers ranging from 1-999 and soon saw that the warehouse value was an alphabetic field? Huh? A little more digging around and I soon found a problem in their database design: They have a database storing warehouse codes, but in some tables they are stored as two character alphameric values, sometimes as three character alpha's and other times as 3 character numerics! :/

Now, if that doesnt set off all kinds of alarm bells in your brain, then you probably want to stop reading and go and make a nice cup of tea and watch a reality show on the Discovery channel. But, if you are a techie and just scratched you head and thought "WTF? Who designed that database then?" the simple answer is NOT ME. Obviously, it was designed by a maniac. Sadly, I'm the programmer who has to work with it and it's established in the business so us poor programmers just have to figure out how to best work with it.

Anyway, before I spend too much time moaning about lazy programmers and their crappy database design, this is how I got around it.

RPG Debug display large field content

RPG EVAL

How to view big fat fields using the RPG Debugger

In my on-going mission to to prove that you can teach an old dog new tricks… I learned a neat one today. I was trying to debug an IBMi Web Interface program (IBM RPG/FreeFormat Language) that was blowing the 64k field size limit when reading in an XML string containing hundreds of order lines.

"Yes, I know the upgrading to IBMi 6.1+ will solve the problem and increase the field limit to 16MEG (Yum!) but the client is on v5r4 and I’m stuck with that….”

So, after a little XML cleanup routine I go the field size down below the 64k limit but still had a problem with XML validation. More scratching of head and after running around in debug I discovered that my XML-RPG validation routine didn’t like something at character position 34,381 in the string. Of course, when in debug and you look at a variable it only shows you the first 1000 character of any fields data:

 

Download and Update WebSphere Development Studio Client WDSC7 for free

download WDSC RPG Code Editor for free

I'm an IBMi Developer - do I choose SEU, RSE or WDSC?

If your caught between using SEU (Stoneage Editting Utility) and RSE (Really Stupidly Expensive) for your IBM-i code development environment - you have one other glaringly obvious choice - WDSC (Wonderful Double Super Codetool).

Personally, I like WDSC because its $FREE. This was the last iteration of the Websphere Development suite before IBM, rebuilt and rebranded it as the 'Rational Developer Tool' and decided to charge $900 per copy. For Freelance RPG Programmers like me that just puts the tool completely out of my budget. I pay one third of that for a license for every piece of Microsoft Software out there... but enough of my gripes about IBM's ridiculous software costs for us freelancers.

using %REPLACE to Scan Replace in RPG4

replace my head with a younger more handsoe one please

So, I want to do a SCAN and REPLACE in RPG - What's the easiest way?

Using the %REPLACE built in Function is easy and efficient allthough code wise its a little cumbersome:

// Update email address                                  
Dow %Scan('L#EMAIL': %TRIMR(SrcDta )) > *Zeros;          
    SrcDta  = %Replace(%trimr(ParmEmail) 
              :SrcDta 
              :%Scan('L#EMAIL' : %TRIMR(SrcDta ))
              :7);    
EndDo;                                                   

In this example, I am scanning a program variable (SRCDTA) looking for any occurence  of the word "L#EMAIL" and replacing it with the contents of "PARMEMAIL". The email value is %TRIM'ed which means that any blanks at the end of the email address are ignored.

I use a similar process in the PROJEX Service Program and then you can easily run a SCAN/REPLACE on a single line of RPG code.

So lets say I wanted to scan for the word 'bob' and change it for the word 'Fred' within a string i could do it like this using this Prototype:

$ResultVariable = #Scanreplace ( $OriginalVariable : 'bob' : 'Fred' )

Much simpler than using the complex %REPLACE BIF I'm sure you will agree. 

Here's the Prototype:

How do I right adjust a numeric into an alpha field using RPG4?

Dont throw those lovely boxes out Mummy!

I have a NUMERIC in an RPG program that I want to move RIGHT into an Alpha field. In the old days of RPG3 I would use MOVE but in the new days of RPG4 I have a couple of more flexible solutions.  So, lets say we have a numeric field containing the number 1234, defined as an signed numeric 10 long it would be stored as - 0000000123. And a Result field which is a big long alpha field:

d $Numeric          s             10s 0 inz(1234)

d $Result           s             10a   inz(*BLANKS)

Some programmers will forget that $variable = $something else is actually eval $variable = $something because the eval is silent  :)
 
We have two easy ways of transforming using RPG4:

With Zero Suppression

If we want to be  tranformed right justified  as  '          1234'' then:

Eval(r) ResultVariable = %char($Numeric);

Doing Loop the Loops in RPG3 and RPG /Free

rpg do loops

So, whenever possible, if I'm editing some old RPG3 or RPG400 code I spend a coffee* cleaning the code up to a more readable form:

  • Use CVTRPGSRC if its old RPG3 stuff
  • Change '1' to *ON
  • Change '0' to *OFF
  • Change Z-ADD to EVAL
  • Insert comments where applicable
  • put some spaces between subroutines and blocks of functionally similar code
  • Then its into WDSC7 and convert to /FREE

Websphere is old technology but I'm not going to spend over $800 on IBM's Rational Developer for i version... c'mon IBM come up with a sensible price for a source code editor. Obviously, the most sensible is FREE.

DO LOOPS are something that sometimes make me scratch my head when uplifting to RPG4. So just for my reference here is an example of the exact same code in RPG3, RPG400 and RPG4 (or RPG ILE as its sometimes called).

 

RPG3

Convert RPG %DATE into a signed numeric

To convert a DATEFIELD to this 8s0 field with no ‘/’ or ‘-‘ do this:

 

 

D  USADate                    8s 0

USADate = %dec(%char(DateField:*iso0):8:0);

 

Or another even neater function is this:

 

USADate = %uns(%char(DateField:*USA0));

 

I prefer this %UNS built in function, since it does not require me to specify length and decimal position parameters. 

 

What is %UNS?


%UNSH (Convert to Unsigned Format with Half Adjust)

 

%UNSH(numeric expression)

%UNSH is like %UNS except that if the numeric expression is a decimal or a float value, half adjust is applied to the value of the numeric expression when converting to unsigned type. No message is issued if half adjust cannot be performed.

 

*..1....+....2....+....3....+....4....+....5....+....6....+....7...+....

add days to 100 Year date from EXCEL using RPG

vintage computing

So, we had a file coming in from an external partner today – containing a date in Julian Format. Or what I initially thought to be Julian format. 

[quote=Some Website Out There]Many applications (especially mainframe systems) store dates in the Julian format, which is a 5 digit number, consisting of a 2 digit year and a 3 digit day-of-year number.   For example, 24-August-1999 is stored as 99236, since 24-August is the 236th day of the year...[/quote]

Then when I looked at the date in question I realised the value was 42345 but this was representing an actual date of December 8th 2015! So, this clearly isnt a Julian date... Hmmm...

Cutting to the chase, it turns out that this date is stored in what the customer refers to as '100 year date format' which is basically the number of days since the last day of 1899. Which just goes to show that those crazy Victorian's where utter whackjobs. 

Luckily its simple to figure out programmatically using RPG4:

 

free rpg code editor for windows - Visual RPG Express

free rpg3 and rpg4 code editor

I'm a big fan of WDSC7 (Websphere Development Studio Client for Windows) for my RPG/CL/DDS/SQL/whatever coding when I'm using windows.. but there are a few others out there. I would love to use IBM's RDi (Rational Developer for IBM i) but while IBM insist on charging around $900 for the program it puts it firmly out of my wallet range. Quite frankly, its such an utterly ridiculous price for a piece of software I cant ever see myself championing it.

I found this article over at Midrange News:

[quote]Visual RPG (aka "CodeStudio") is a PC Windows XP-based editor for RPG IV, DDS, and other languages. It was original sold to AS/400 developers as a "CODE/400 for Windows" product when there was no Windows-based "CODE/400" (they only did an OS/2 version). Later it was renamed to "CodeStudio" and that trademark was purchased by a multi-national corporation and the product name was changed back to its original "VisualRPG" and provided at no-charge with no support. It uses FTP to pull down the source from the host and save it back up to the host, hence the source statement change dates are lost.[/quote]

Here are the instructions (over a decade old but if they help someone out then TADA!)

RPG4 is not a new Rocket Propelled Grenade

learning rpg programming language

I occasionally get an email from RPG programmers out there in IBMi-Land who have spent the last decade gently plodding along in RPG3 and RPG400 and now face the daunting prospect of having to hone there RPG skills and learn RPG4 (or RPG-LE or /FREE as its also called). So, I’ve quickly cobbled together links and information from various sources on the web and dropped it here for reference.

What Software Tool do I use to write RPG?

Three main choices really:

Exit Programs, Audit Trails and APIs

Question: All the Exit Program examples I see are in the C Language. Is it possible to write Exit Programs in RPG?

Answer: Yes, but you must write a data structure as same as it in the C header file. The C header file is H/QSYSINC. This include is also available for other languages in QSYSINC/QRPGSRC, QSYSINC/QRPGLESRC, QSYSINC/QLBLSRC, and QSYSINC/QCBLLESRC.

Question: I have been asked to find out how AS/400 users can change their passwords using a web browser application.  We will synch user info to the NT Domain server to enable validation and signon.  The AS/400 passwords expire every 30 days.  The users must be able to maintain their passwords without leaving the web application (a combination of Cold Fusion, Javascript and HTML).  Off the shelf packages are OK, or IBM supplied API that support some sort of encryption (we don't want passwords xmitted over the internet in the clear.)  Any and all suggestions are appreciated. (12/99)

as400 users become iseries users become IBMi users

rpg code example seu pdm

User profiles are such a wonderful and flexible part of the IBMi operating system. Group profiles, Security levels, authorization lists.... good stuff:

Managing User Profiles

Question I am having a problem with user profiles disabling randomly.

Answer 1. It sounds like someone has used the Security Toolkit to activate the "automatically disable inactive profiles" option. That's not really it's name. It's name is "Analyze Profile Activity" on the menu option, which sounds benign enough, but in fact it will initiate this scheduled auto-disable.

From the Security Toolkit (GO SECTOOLS) choose option 4 (ANZPRFACT) and set the number of days to *NOMAX. This will prevent any profile from being automatically disabled by this new feature.

Alternately, you could leave the auto-disable at 90 days, and then use options 2 & 3 on that same menu to exclude certain profiles from being disabled.

Zend Framework for RPG programmers

The Zend Framework is a neat thing for letting my RPG programmers brain run a little wild. Yes it's object Oriented and not (necessarily) that easy to adapt to the linear model that us old schoolers may be used to. But does that mean we can’t adopt it?robbie was an rpg programmer

"Hell No!"

- "Adapt and Survive" is the motto of all us lucky people who have grown up through the AS400 years.

RPG Code Hints and Tips – using slash Title Mainline

I'm in the middle of refactoring a load of old Projex400 RPG code from RPG3 upto RPG4 and using all the modern BIF's. It's a fun thing to do in the evening while I'm hiding in an hotel room in rainy Ohio. But I digress...    Inevitably my first compile will fail and I normally want to jump to the 'C' specs to have a look at what I've got wrong... this normally involves repeatedly pressin the page-down key until my finger tip gets numb.

rpg programming help

But wait, I stumbled across a neat little code snipper that saves time and looks good in the code as well.

Use the /title command in the source code to add a comment to your spool file listing.

This was used a lot in the old days of programming along with the /page and /eject codes. I have started using the ‘/title mainline’ as the first line of ‘c’ specs in RPG4/Column-Based, or just before the /FREE in RPG4/FreeFormat.

Why?

RPG3 versus RPG400 versus RPG4 in all its glorious free format styley

i love programming in RPGLast night I was asked what the differences between RPG versions were. IBM have gone out of their way to make the version naming and releases differences so quirky, dirty and convuluted that I'm not even sure they know what to call the current incarnation of this beautiful programming language.

Anyhooo, here's a quick splurge and some code examples of  RPG3, RPG400 and RPG4 (FreeFormat)

RPG3 looks like this:

 

Web Services in native RPG straight from IBMi

 

I've long been a follower of Mr Aaron Bartell - a nice guy and one of the most dynamic IBMi developers I have come across for a while.

His company, Krengel Tech, have some neat functions that simplify getting good old AS400 data (yes, I know its an iSeries or IBMi but sometimes I just like to type the old names :) )  out there to the Internet Super Web Highway Thingie...

In simplistic terms, RPG-XML Suite essentially is an RPG service program that allows RPG programmers the ability to compose, transmit, and parse XML along with many other supporting subprocedures. These three capabilities fully equip an RPG programmer to offer (or provide) web services on the iSeries as well as call (or consume) web services on remote machines.

squish out blanks in a string using RPG

dont squish me

So, as part of an IBMi project which is rebuilding a bunch of source libraries on the development machine, I always think backup before touching. Obviously an offline backup is first and foremost and then I thought "Wouldnt it be nice if I could copy all this source code to the IFS for easy referencing with NOTEPAD?"

The answer was YES... in true Projex style this turned into a command and CPYSRC2IFS was born.

Pages