Start of change

%SCANR (Scan Reverse for Characters)

%SCANR(search argument : source string {: start position {: length}})

%SCANR returns the last position of the search argument in the source string, or 0 if it was not found.

The start position and length specify the substring of the source string to be searched. The start position defaults to 1 and the length defaults to the remainder of the source string. The result is always the position in the source string even if the starting position is specified.

The first parameter must be of type character, graphic, or UCS-2. The second parameter must be the same type as the first parameter. The third and fourth parameters, if specified, must be numeric with zero decimal positions.

When any parameter is variable in length, the values of the other parameters are checked against the current length, not the maximum length.

The type of the return value is unsigned integer. This built-in function can be used anywhere that an unsigned integer expression is valid.

If the search argument contains trailing blanks, the scan will include those trailing blanks. For example if 'b' represents a blank, %SCANR('12b':'12b312') would return 1. If trailing blanks should not be considered in the scan, use %TRIMR on the search argument. For example %SCAN(%TRIMR('12b'):'12b312') would return 5.

For more information, see String Operations or Built-in Functions.

%SCANR Examples

Example using %SCAN and %SCANR together

In the following example, %SCAN and %SCANR are used with the same operands. Since %SCAN searches from the beginning and %SCANR searches from the end, they will return different results if there is more than one occurrence of the search argument in the search string.

  1. The value returned by the %SCAN built-in function is 1.
  2. The value returned by the %SCANR built-in function is 24.
  3. The values are not the same. This indicates that there are at least two occurrences of "VALUE" in the string.
   string = 'VALUE 9.56, VALUE 7.3, VALUE 4.71';
   p1 = %SCAN ('VALUE' : string);  1 
   p2 = %SCANR ('VALUE' : string);  2 
   if p1 <> p2;
      moreThanOne = *ON;  3 
   endif;
End of change