Wednesday, April 04, 2007

RPG on the AS/400 - %Subst

Wanting to switch to RPG IV/LE and maybe /FREE, but afraid of string manipulation?

RPG IV makes it easier than ever.

Want to replace the last three characters of a string? Used to using MOVE and making sure your fields actually do line up? Well wash those worries away.

myString = 'ABCDEFGHI';
%Subst(myString:7:3) = 'ABC';

DSPLY myString;

The result? ABCDEFABC

Wait, you didn't know you could use %Subst on the left hand side? Yes, and you can use it on both sides.

%Subst(myString:1:3) = %Substr(myString:4:3);
DSPLY myString;

The result? DEFDEFABC

Pretty fancy huh? Well, let's try something a bit more fun.
Want to generate some alphanumeric strings based on an original string and a number, well, based on what we saw in a previous blog post, we can put that together.

myNum = 177;
myName = Tojosan;

myCodeWord = %Subst(myName:1:4) + %Char(myNum);
DSPLY myCodeWord;

The result is Tojo177. You could get much fancier by using the last three digits of a number for example by %Subst over the %Char result.

Well enough on the simple stuff. Soon I'll be covering MONITOR, one of my new favorite saving commands.


No comments: