Occurs()

It might be better if this function were called "occurrences" because that's what it counts. OCCURS() returns the number of times one string is contained in another. For example, if you want to know how many times "iss" occurs in "Mississippi", OCCURS() is just what you need.Let's say (in spite of relational theory) you've stored a series of codes in a memo field, separated by spaces, and you want to know how many codes a given record has. Use OCCURS() to count the spaces, then add 1 to get the result.

Usage

nReturnValue = OCCURS( cSearchFor, cInString )
The return value is the number of times cSearchFor appears in cInString. OCCURS() is case-sensitive, so you may need to apply UPPER() to one or both strings.Together, with AT() or RAT() and SUBSTR(), LEFT() or RIGHT(), OCCURS() is handy when you're parsing character strings. For instance, if you allow a user to enter a comma-delimited list of values to search for, and you need to break up that list into a series of individual items, you can do it like this:

Example

* cInput is the input string
* store the results in array aValues with one item per element
nItems=OCCURS(",",cInput)
DIMENSION aValues[nItems+1]
 
FOR nCnt=1 TO nItems
   * find the first comma
   nCommaPos=AT(",",cInput)
   
   * grab the string before it
   aValues[nCnt]=LEFT(cInput,nCommaPos-1)
   
   * shorten the string
   IF nCommaPos<>LEN(cInput)
      cInput=SUBSTR(cInput,nCommaPos+1)
   ELSE
      cInput=""
   ENDIF
ENDFOR
 
* now take last item
* check just in case there was a trailing comma
IF EMPTY(cInput)
   DIMENSION aValues[nItems]
ELSE
   aValues[nItems+1]=cInput
ENDIF
The example uses OCCURS() to figure out how many commas there are and then assumes that's one less than the number of items. It then loops through, lopping off one item at a time and shoving it into the array. At the end, it checks to make sure there really is an item after the last comma before grabbing what's left of the input.

See Also

$, At(), InList(), Left(), RAt(), Right(), SubStr(), Upper()


Back to Table of Contents

Copyright © 2002-2018 by Tamar E. Granor, Ted Roche, Doug Hennig, and Della Martin. Click for license .