April 13, 2004
A language comparison: postcode splitting in RPG and Python

As you might have guessed from my last post, I've got some postcode parsing to do. The requirement is pretty simple; I have to parse a postcode into an outcode and (optional) incode. The good new is that I don't have to perform any validation - I just have to chop up what I've been provided with in a vaguely intelligent manner. I can't assume much about my import in terms of formatting, i.e. case and embedded spaces. But this isn't the bad news; the bad news is that I have to do it all in RPG. :-(

Nevertheless, I threw a little Python function together to clarify my thinking. I often do this when I'm coding something nasty in Java. It's worthwhile to make sure that the algorithm that I'm using works before grappling with Java, and Python is perfect for this. And given RPG's awkwardness, it's worth prototyping even the simplest algorithm - you don't want to spend all that time cutting code only to find that it doesn't work even in principal!

Anyway, the Python function:

def parse_uk_postcode(postcode):
    postcode = postcode.replace(' ', '').upper()
    if len(postcode) <= 4:
        return postcode, ''
        return postcode[:-3], postcode[-3:]

(A syntax highlighted version with unit tests here: postcode.py.)

Then I put the RPG version together: postcode.rpg. It's much longer and ugler, but it seems to work.

This is a very tolerant way of dealing with postcodes. It will accept just about anything. In my case, that's what I want; I'm pulling data in from a big SAP database. If that's inaccurate, it's definitively inaccurate. ;-)

In other cases, you might want to validate the postcode. Even here, you have more than one choice. One approach is to make sure that the postcode follows the rules as specified at the Universal Postal Union and the UK Govornment Data Standards Catalogue. I've coded this up: postcode_strict.py. This might even be useful to someone! (I didn't bother with an RPG version of this; I don't need one, and life's too short for writing this kind of thing in RPG if you don't really need it.) The other approach would be to validate against a list, should you have one. Duncan Gough steered me towards Brainstorm's downlaodable postcode lists, but these contain outcodes only.

Oh, BTW, any suggested improvements to any of these functions in any of these languages would be gratefully recieved, as would Java versions for comparison.

Update 14th April: Alan Green's done a Java version of the simple version. Thanks, Alan!

Also, I've spotted a bug in the strict version, which I'll fix at lunchtime. Basically, I'm using the list of characters valid at the 4th position to check characters at both the 3rd and 4th positions. This is incorrect; there's a different list of valid characters for the 3rd position. :-(

Another update 14th April: It's fixed, it works now, and I've even added a docstring or two. Look at postcode.py, or download a zip file containing postcode.py.

Posted to iSeries by Simon Brunning at April 13, 2004 04:57 PM

Damn, I thought I'd just look at the RPG, because I didn't really know what RPG looked like. I tried to puzzle through it a bit, but quickly decided that was a futile and depressing task. What painful language -- I pity you!

Posted by: Ian Bicking on April 13, 2004 05:30 PM

And this is RPG IV, written in a fairly modern style. You should have seen RPG III!

Posted by: Simon Brunning on April 13, 2004 05:32 PM

I _have_ seen RPG III. And older versions, too - or whatever was running on /36 machines. Ugh. Those are memories you don't want to come back at night to haunt you ...

I don't want to even think about how that code would have looked on an old /36 machine ...

Posted by: Georg Bauer on April 13, 2004 06:29 PM

When I was working on a Cool:Plex/Websydian web application, this was a favorite trick of mine.
The action diagrammer in Plex is only slightly better than 2E and both of these are worse than straight RPG. Saved me lots of headaches.

Posted by: j on April 13, 2004 10:12 PM

Holy cow, that's ugly!

Have you ever been tempted to write Python code that generates the RPG for you? There's got to be a way of dulling the pain somehow. Having experimented heavily with Python code generators over the past few months I'm determined to use a generator next time I have to work with an inflexible language such as Java or C.

Posted by: Simon Willison on April 13, 2004 10:33 PM

Woah! I thought for a moment there that I had somehow clicked through to postcode.asm by mistake :)

Posted by: Alan Green on April 14, 2004 06:28 AM

Code generation would be tempting - except that I'm not allowed to install anything on the PC I'm using, let alone on the iSeries I'm workign with. I'm in real green-screen land here; the only tools that they expect a developer to need (or want) is a 5250 emulator[1], PDM[2], SEU[3] and possibly SDA[4]. Sigh.

[1] http://www.seagullsoftware.com/products/bluezone_terminal.html
[2] http://publib.boulder.ibm.com/iseries/v5r2/ic2924/books/c0921332.pdf
[3] http://publib.boulder.ibm.com/iseries/v5r2/ic2924/books/c0926050.pdf
[4] http://publib.boulder.ibm.com/iseries/v5r2/ic2924/books/c0926040.pdf

Posted by: Simon Brunning on April 14, 2004 09:31 AM

It's Advantage 2E and Advantage Plex these days. Jay! ;-)

The action diagrammer, worse than SEU? Hmmm, I'm not sure. Worse in some ways, it's true - non text-based code editors are always pretty nasty. But the prompting is good, so you can be fairtly productive. Besides, there's *so* much less code to write...

Posted by: Simon Brunning on April 14, 2004 09:43 AM

From BlueZone's docs:

> Enter the BlueZone. It's the state-of-the-art in thin, fast, secure, full-function Web-to-host
> and desktop terminal emulation for IBM mainframe (3270), iSeries (5250) and UNIX/DEC/VMS
> (VT). And every BlueZone license includes the secure File Transfer Protocol (FTP) feature!

Sounds to me like you can write the emulator on your PC at home then FTP the results back. Right?

Seriously, is it really just that they don't want you messing up their PC by installing random junk, or would they truly object to your using whatever tools (including code generators) were the best for the job. If the latter, then I seriously question the competence of the people in charge. There are LEGITIMATE reasons for choosing one technology over another, but that isn't one of them.

Posted by: Michael Chermside on April 14, 2004 02:35 PM

re: name of the month. I really can't be bothered to keep track. I knew you would know what I meant. (and gently correct me with a smiley)

I wasn't too clear I guess about the other point. It's the op-codes that cause a problem in Plex/2E. While SEU is a pain at least I have the full gamut of RPG to draw upon. For instance, remember the pseudo-xml parser in 'Forge'. That would be very hard in Plex/2E and the RPG version was pretty fragile when I turned it over to you.

The Action Diagrammer in 2E is waaaay faster to use (no mouse) but lacks some features of Plex. Plex is nice because you can define your own contexts (Plex calls them variables) but actually using the Action Diagrammer is extremeely frustrating. Modal dialogs, trees that don't remember their state (i.e. they are fully collapsed everytime you open them), lots of dragging and dropping with non-obvious drop targets. I could go on and on. Granted this was 3 years ago but I can't imagine it being much better becuase some of the problems I remember from beta-testing KT. (I was a Synon employee at the time.)

Instead of generating RPG, I've been using Python to analyze Synon generated RPG. We are replacing the inventory mgmt piece of our custom app with a different one. (both are purchased packages) The code was written a long time ago before Synon had the LCL and NLL contexts plus it was partially written by noobs that didn't understand the nuances of WRK context. Therefore we have alot of code that does a 'RTV All' to a file we are replacing and moves everything to a WRK context. I'm using Python to find the calls to the RTVOBJ's in the RPG and figuring out which context that each of the output parms belong to and then finding where else in the program that it is being used. That way we can tell that only the 'description' or whatever is being used and we can replace the 'RTV all' with a 'RTV Description' over the new file.

Posted by: j on April 14, 2004 02:52 PM

It's a big company that I'm working for, and the corporate PCs are locked down by default. Getting permission to install stuff would probably take longer than the lifetime of the project. And I'm not allowed to plug *anything* into thier network.

All this is irritating, but understandable. I'm working for a large music publisher, and there have been thefts and pre-release leaks of music before.

Besides, green-screeners *really* don't think you need anything but a 5250 emulator, PDM, SEU and possibly SDA. After all, it's all *they* use...

I suppose that the reason that I've not had problems with 2E's action diagram editor (I've never used Plex) is that I've always used 2E in a linited domain. I'd never try anything like a pseudo-xml parser in 2E - I'd do it in RPG, and drop it into the model as a USRPGM. For the kind of problem that you would do in 2E action dialgrams - database access and updates, financial calculations - I'd always found 2E's action diagrams (and action diagram editor) adequate.

But then, I'd probably nhave felt the same about RPG before getting used to Java and Python.

LCL and NLL contexts? Cool - 21st century!

Posted by: Simon Brunning on April 14, 2004 03:06 PM

There's the rub. I _had_ to use the Plex AD tool because we were generating C++. I couldn't drop down to RPG. But in general, I agree. If you can't make Synon do what you want, write some user source or a user pgm. I think 90% of Synon models in existence has a '*MOVE RIGHT' user source under the '*Built in Functions' entity.

At the risk of completely boring your regular readers, I'll give you a little bg on LCL and NLL.

NLL is a throw away context. So if you don't want to use the output value from a call to a function put it to the NLL context and it is effectively thrown away. You can't use NLL context as an Input parm anywhere. But under the covers, the correct RPG is generated to fields named YN####.

LCL creates RPG fields named YL#### but scopes them to the function. So you can use 'LCL.Order nbr' in a PMTRCD and when it calls a RTVOBJ you can use 'LCL.Order number' inside the RTVOBJ and it will get a different YL#### field name. So even tho RPGIV only has global variables, Synon makes them look like local variables by using a different RPG field name.

Posted by: j on April 14, 2004 03:27 PM

Hey, don't go boring my regular readers, Jay - that's *my* job!

Posted by: Simon Brunning on April 14, 2004 03:41 PM

There is a %len bif available on the iseries, don't know why you didn't use it. You also don't need to use fixed column any more for c-specs. Free format has been available for some time.

Comparing RPG to other languages doing string manipulation is a little deceptive. RPG has always been weakest in this area. It would be a more realistic comparison if you were doing database manipulation.

Posted by: Mike Rogers on April 14, 2004 05:34 PM

The %len function counts spaces, so it doesn't do quite what I need. I could have used %len(%trim()) - I did consider it - but that struck me as no simpler than using %scan, which does *exactly* what I need.

Free format would have been nice, but the client for whom I'm working doesn't want it used.

It's certainly true that RPG is stronger at database manipulation than it is at string manipulation - in some ways, stronger than Python - but I don't *have* any examples of the same task coded in both RPG and Python. This is a real workd example, not a manufactured one.

Thanks for the feedback, Mike!

Posted by: Simon Brunning on April 14, 2004 06:40 PM

Please don't forget people that RPG is not another programming language. That code that Mr Brunning has written will be FAST. It also will work on each record of a HUGE database of records with just a few more lines of code. In other words it will easily integrate with the largest software project undertaken by mankind, so far : OS/400.

Having said that...I have not looked back since moving to Python/Zope :-)

But I would really like to see this stuff integrate nicely with DB2/400. Because scaleability only comes through gradual evolution, and the iSeries has this in spades.

Posted by: jorjun on April 26, 2004 09:59 AM

cool blog.

Posted by: Martin on October 25, 2004 04:55 PM

I saw in your comments that you would feel tempted by a code generator for RPG. Actually, I have done this on my site - http://rpgcrud.ro and it works really well - we have several houndreds of generated code.
Basically I tried to create a code generator that will completely write the code for me (so not only parts of the code but whole dds and programs) for simple CRUD applications. You just have to provide the fields of your table and the code is created in an instant. With it you are able to insert/change/delete/view/copy/search and much more in a friendly (and customizable) interface. If you would like to give it a try (by the way it's completely free & open source) I would be more than happy to hear your comments and guide you in using it. And if you have some features that you would like to have I would be very happy to provide you with them.

Thanks and sorry for the "advertising".

Posted by: tzighi on November 15, 2007 11:02 PM


Handy snippet, thanks. Never occurred to me to track 3 chars back instead of trying to work out all the various rules :o

I've ported this to PHP, in case anyone is interested, here:



Posted by: alex on April 29, 2008 10:28 AM


Martha Louise, who is the only daughter of King Harald and Queen Sonja, gave up the title of 'royal highness' upon her 2002 marriage to writer Ari, and has a reputation for not standing on ceremony.

Posted by: spamsDaug on October 10, 2008 09:08 PM

Buy Accutane Accutane is best known and most effective medicine used for treating severe acne. It is a variation of Vitamin A, which helps regulate the work of oil glands in the skin, making it less greasy and cleaner. It is usually prescribed after other acne drugs or antibiotics. If you want to have beautiful clean skin and get rid of acne, Accutane is definitely your medicine!

http://pendosam.com/?product=accutane Buy Accutane

Posted by: UrbabyFub on October 21, 2008 04:46 AM

pornhup com - hqsex - teenel com - myporno tvnet - assoass -

Posted by: balabo3_mm on May 20, 2009 02:10 AM

pornhup com - hqsex - teenel com - myporno tvnet - assoass -

Posted by: balabo3_mm on May 20, 2009 02:10 AM

pornhup com - hqsex - teenel com - myporno tvnet - assoass -

Posted by: balabo3_mm on May 20, 2009 02:10 AM

buy cheap viagra
viagra cialis
cheap viagra
purchase viagra online
female viagra

Posted by: viagra prescription on June 19, 2009 09:38 AM

Very interesting site. Hope it will always be alive!
buy , , , buy , , buy , cheap , , cheap , buy ,

Posted by: John456 on July 6, 2009 01:55 AM
Post a comment

Email Address:



Remember info?