|
|
|
| 9 January 2002 | Occurs() |
| Author | ScriptLogic Corporation |
| Action | Calculates the number of times a string occurs within another string, element or all the elements of an array. |
| Syntax | Occurs (ExpC1, ExpC2) |
| Parameters | |
| Remarks | A good example of how to use the new VarType() function and FOR EACH...NEXT command. Also demonstrates how a function can recursively call itself. |
| Returns | A number representing the total number of times that ExpC2 was found in ExpC1. |
| Dependencies | None. |
| Examples | $string='babbabababbc' $search='ab' $result=occurs($string,$search) ?'"$search" occurs $result times in the "$string" string' $array='abc123','def123','123ghi123','jkl123','mno123','babbabababbc' $search='ab' $result=occurs($array[5],$search) ?'"$search" occurs $result times in the only searched element of the array' $array='abc123','def123','123ghi123','jkl123','mno123','babbabababbc' $search='123' $result=occurs($array,$search) ?'"$search" occurs $result times within all elements of the array searched' |
| Source |
FUNCTION Occurs ($expc1, $expc2)
; Author: ScriptLogic Corporation
; $ExpC1 a text string, array element or entire array to search.
; $ExpC2 is the expression we are attempting to find within the
; string or the elements of the array.
; Returns: the total number of occurrences that $ExpC2 was found,
; or 0 if not found anywhere within the elements of the array.
DIM $count
SELECT
CASE VarType($expc1) >= 8192 ; array
DIM $element,$index
$index=0
FOR EACH $element IN $expc1
$count=$count+occurs($element,$expc2) ; recursive call
$index=$index+1
NEXT
CASE VarType($expc1) = 8 ; string
DIM $lenexpc1, $lenexpc2, $loc
$lenexpc1=Len($expc1)
$lenexpc2=Len($expc2)
$count=0
WHILE Len($expc1) > 0
$loc=InStr($expc1,$expc2)
IF $loc
$count=$count+1
ENDIF
$expc1=Substr($expc1,$loc+$lenexpc2,$lenexpc1)
LOOP
CASE 1
; ** not a string, element or array - not supported by this function
ENDSELECT
$occurs=0+$count
ENDFUNCTION ; - Occurs -
|
|
|
|