Toby’s RPG Snippet to Scan Replace is mental but sexy

  • Home
  • /
  • Blog
  • /
  • Toby’s RPG Snippet to Scan Replace is mental but sexy

July 19, 2010

Toby’s RPG Snippet to Scan Replace is mental but sexy

By NickLitten

July 19, 2010

replace, RPG, scan, snippet

I was recently copied on an interoffice email from a fellow RPG chappie (he’s quite sharp actually despite his funny mockney accent).

Aforementioned Mockney-Coder was modifying a recent program, written by a legacy RPG3 programmer (these creatures are very rare to find in the wild and we have placed them on the endangered species list).

Toby's RPG Snippet to Scan Replace is mental but sexy 1Amazingly, there are still a handful of RPG Programmers out there who still write in RPG3, even though it was upgraded to RPG400 in 1993(?) so if they havent upgraded to RPG400 yet then the chances of getting up to RPGLE and then to RPG/FREE are slim to none.

However, Mockney-Coder is a smart chap and he mentioned a neat little alternative piece of RPG/FREE code that could be used to replace the large subroutines and array handling code chunks needed to perform the same thing in the old fashioned and clunky column based RPG varieties. After all, Variety is the spice of life…

all names have been removed to protect the innocent 😉

Don’t know if you are using /Free or not because I have been hiding a lot lately.

I transposed a Serial Number from a left adjusted alpha field to a zero padded right adjusted field with no Arrays or Data Structures and thought you might find the code use full/Interesting as you do this kind of thing a lot. So the D specs are the same, they are just usually more descriptive/Longer because the code can take longer values so…

RPG Code Snippetd Pos s 2 0 inz(*zero)
 d Len s 2 0 inz(*zero)
 d SernD2 s like(Sernds) inz('000000000000000’)

Pos=0;
 Pos=%scan(' ':SernDS);
 If Pos<>0;
    Pos=(Pos-1);
    Len=(15-(Pos-1));
    SernD2=%replace(%subst(SernDS:1:Pos):SernD2:Len);
 Endif;

This is looking for the First Blank in the Screen Field (SernDS), in this case ‘5000012 ‘ so Pos=8, sub 1 from that to store the last position before the blank.

Len is the remaining positions (position where you want to start overlaying the zeros with ‘5000012’) in SernD2 (Work Field for Chains etc.) which is position 9.

And %Replace is just ‘change this bit of this thing to this instead’.

So, any serial numbers that are stored as ‘1234567859 ‘ will be transposed to 00000123456789 which is handy as this database stores them both ways.

Cheers

I like this mockney code example – but it gives me a bit of a headache trying to figure it out:

Any simpler examples out there?

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

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

>