Wednesday, September 17, 2014

Difference between %check() and %checkr() in RPGLE?

This post is written for illustrating the difference between RPGLE functions %CHECK() and %CHECKR().
%check() parses left to right, %checkr() parses right to left.

%CHECK()

First position in the data that is to be searched ,contains a character not in the list of the characters in the compare value.

%CHECKR()

Last position in the searched-data that contains a character not in the list of the characters in the compare value.(Search begins with the right-most character and proceeds to the left. Basically does the same thing as %CHECK() but starts from right to left in the string data.

This is the example for your better understanding.
D string S 15
D pos1 S 10 0 inz( 0 )
D pos2 S 10 0 inz( 0 )
/free
String = 'AABC1ABD2AB3A' ;
// ^ ^
// The above characters will trigger mismatches for
// %CHECK() and %CHECKR.
Pos1 = %check ( 'ABCD' : %trim( String )) ;
Pos2 = %checkr( 'ABCD' : %trim( String )) ;
// Will display the index value "1 3"
dsply ( %subst( %trim(String): pos1: 1 ) + ' ' +
%subst( %trim(String): pos2: 1 ) );
*inlr = *on ;
return ;
/end-free
Given the example, %check() will return 5 and %checkr() will return 12.