POSITV: Finding the Beginning of a Variable Length Substring

How to:

Available Languages: reporting

The POSITV function finds the starting position of a substring within a larger string. For example, the starting position of the substring DUCT in the string PRODUCTION is 4. If the substring is not in the parent string, the function returns the value 0. This is similar to POSIT; however, the lengths of its AnV parameters are based on the actual lengths of those parameters in comparison with two other parameters that specify their sizes.

Syntax: How to Find the Beginning of a Variable Length Substring

POSITV(source_string, upper_limit, substring, sub_limit, output)

where:

source_string

Alphanumeric of type An or AnV

Is the source string that contains the substring whose position you want to find. It can be the string enclosed in single quotation marks ('), or a field or variable that contains the source string. If it is a field of AnV format, its length is taken from the length bytes stored in the field. If upper_limit is smaller than the actual length, the source string is truncated to this upper limit.

upper_limit

Integer

Is a limit for the length of the source string.

substring

Alphanumeric of type An or AnV

Is the substring whose position you want to find. This can be the substring enclosed in single quotation marks ('), or the field that contains the string. If it is a field, it can have An or AnV format. If it is a field of type AnV, its length is taken from the length bytes stored in the field. If sub_limit is smaller than the actual length, the source string is truncated to this limit.

sub_limit

Integer

Is the limit for the length of the substring.

output

Integer

Is the name of the field that contains the result, or the format of the output value enclosed in single quotation marks (').

Example: Finding the Starting Position of a Variable Length Pattern

POSITV finds the starting position of a trailing definite or indefinite article in a movie title (such as ", THE" in SMURFS, THE). First TRIMV removes the trailing blanks from the title so that the article will be the trailing pattern:

DEFINE FILE MOVIES
  TITLEV/A39V = TRIMV('T',TITLE, 39,' ', 1, TITLEV);
  PSTART/I4 = POSITV(TITLEV,LENV(TITLEV,'I4'), ',', 1,'I4');
  PLEN/I4 = IF PSTART NE 0 THEN LENV(TITLEV,'I4') - PSTART +1
                    ELSE 0;
END
TABLE FILE MOVIES
  PRINT TITLE
   PSTART AS 'Pattern,Start' IN 25
   PLEN AS 'Pattern,Length'
BY CATEGORY  NOPRINT
WHERE PLEN NE 0
END

The output is:

                         Pattern  Pattern
 TITLE                     Start   Length 
 -----                   -------  -------
 SMURFS, THE                   7        5
 SHAGGY DOG, THE              11        5
 MALTESE FALCON, THE          15        5
 PHILADELPHIA STORY, THE      19        5
 TIN DRUM, THE                 9        5
 FAMILY, THE                   7        5
 CHORUS LINE, A               12        3
 MORNING AFTER, THE           14        5
 BIRDS, THE                    6        5
 BOY AND HIS DOG, A           16        3