Progress
Language Reference


INDEX Function

Interfaces
OS
SpeedScript
All
All
Yes

Returns an integer that indicates the position of the target string within the source string.

SYNTAX

INDEX ( source , target [ , starting ] ) 

source

A character expression.

target

A character expression whose position you want to locate in source. If target does not exist within source, INDEX returns a 0.

starting

An integer that specifies at which left-most position in the string to start the search. For example, INDEX("abcdefabcdef","abc",6) returns 7.

EXAMPLES

For this example, you must enter 1, 2, 3, 4, or 5. The INDEX function checks if the digit exists in the string "12345".

r-index.p
DEFINE VARIABLE x AS CHARACTER FORMAT "9"
    LABEL "Enter a digit between 1 and 5".
DEFINE VARIABLE show AS CHARACTER FORMAT "x(5)" EXTENT 5 LABEL "Literal"
    INITIAL["One", "Two", "Three", "Four", "Five"].

REPEAT:
    SET x AUTO-RETURN.
    IF INDEX("12345",x) = 0 THEN DO:
        MESSAGE "Digit must be 1,2,3,4, or 5. Try again.".
        UNDO, RETRY.
    END.
    ELSE DISPLAY show[INTEGER(x)].
END. 

This procedure also uses the starting option.

r-index2.p
DEFINE VARIABLE positions AS CHARACTER FORMAT "x(60)".
DEFINE VARIABLE sentence AS CHARACTER FORMAT "x(72)".
DEFINE VARIABLE vowel AS CHARACTER FORMAT "x".
DEFINE VARIABLE found AS INTEGER.
DEFINE VARIABLE loop  AS INTEGER.
DEFINE VARIABLE start AS INTEGER.

FORM sentence LABEL "Type in a sentence"
  WITH FRAME top
  TITLE "This program will tell where the vowels are in a sentence.".

SET sentence WITH FRAME top.
DO loop = 1 TO 5:
  positions = "".
  vowel = SUBSTRING("aeiou",loop,1).
  start = 1.
  found = INDEX(sentence,vowel,start).
  DO WHILE found > 0:
    positions = positions + STRING(found) + " ".
    start = found + 1.
    found = INDEX(sentence,vowel,start).
  END.
  DISPLAY
  vowel LABEL "Vowel"
  positions LABEL "Is found at locations..." WITH 5 DOWN.
  DOWN.
END. 

NOTES

SEE ALSO

LOOKUP Function, R-INDEX Function


Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095