MC Press Web Site Staff
** This thread discusses the Content article: TechTip: Meet a Powerhouse BIF: %replace!0
Guest.Visitor
Hi, Just read the tip and I think a small error has occured... %subst(Answer:3:4) = %subst(Field2:3:4); will give ANSWER = \' XWVU \' and Answer = %replace(Field2:Field1:3:4); will give a result of ANSWER = \'abZYXWVUTSRghi\' To get result Answer= \'abXWVUghi\' is should look something like this Answer = %replace(%subst(Field2:3:4):Field1:3:4) I ran it through the debugger, so I really hope I am right... All the best - Jan
D.Eckersley
Did you catch the Answer = Field1; before the %subst(Answer:3:4) = %subst(Field2:3:4); ?? I think the article is correct. However, I think you are correct, or at least close, with the %replace example. Jim is placing Field2 into 4 positions of Field1 starting at position 3. Answer would then be \'abZYXWghi\'. I believe your solution would result in the desired Answer. You could even simplify it a little: Answer = %replace(%subst(Field2:3:4):Field1:3);
rpg@post.tele.dk
Glasses on - I can see clearly now.... All the best - Jan
J.Martin
BIF %replace revisited by Jim It seems that the more I work with this BIF, the more I find. In my recent technical tip on %replace, I erred in my first example. Here it is again: D Field1 S 9 Inz(\'abcdefghi\') D Field2 S 9 Inz(\'ZYXWVUTSR\') D Answer S 14 /free Answer = %replace(Field2:Field1:3:4); I said that Answer would be \'abXWVUghi\', which is incorrect. Thanks to the sharp eye of JLA (and others), I now realize that the correct answer is \'abZYXWVUTSRghi\'. I fell into the "trap" that would have the starting position (parm 3) and length (parm 4) affecting both the from-string and the to-string. It does not. Here\'s the story: Parm 3 (starting position) and parm 4 (length) affect the to-string first. These two parameters tell the BIF where in the to-string to start removing characters and then how many to remove. After the removal process has completed, the BIF inserts the entire from-string (which may be a sub-string) at the parm 3 location in the to-string. Parm 4 does not condition how many characters to insert. This BIF "replaces" in the sense that it can remove characters and then can insert other characters. In summary then, the %replace BIF has the capability to do the following: 1. Delete characters from a character string (parm 2) only. 2. Insert characters from a character string (parm 1) into a character string (parm 2) without any deletion of characters. 3. Delete characters at a specified location (parm 3) for a specified length (parm 4) in a character string (parm 2) and then insert all of a character string (parm 1) at the same location (parm 3). Back to my example: The correct way to use %replace to get the desired string is the following: /free Answer = %replace(%subst(Field2:3:4):Field1:3:4); // Answer is \'abXWVUghi\' Another possible use for %replace is to emulate the MOVE op-code, where the receiving field is longer than the sending field and padding is not desired. Since MOVE is not available in free format, the use of %replace could be useful. Here\'s an example: D Field1 S 6 Inz(\'ABCDEF\') D TwoChar S 2 Inz(\'XY\') /free Field1 = %replace(TwoChar:Field1:(%len(Field1)- %len(TwoChar) + 1); // Field1 now \'ABCDXY\' It\'s cumbersome, but it gets the job done. With careful use, %replace can be very helpful with your character manipulation needs.
Please login to make comments.
User Rating: / 0
PoorBest 

WHITE PAPERS

The following White Papers can be found at the MC White Paper Center

 

 


   MC-STORE.COM