scripting@wanadoo.nl




Occurs()

http://www.scriptlogic.com/kixtart/FunctionLibrary_FunctionList.aspx


actual FunctionLibrary list on 'Scriptlogic' site printer-friendly mirror of UDF topic on 'scripting@wanadoo.nl' site close
9 January 2002 Occurs()
AuthorScriptLogic Corporation
Action Calculates the number of times a string occurs within another string, element or all the elements of an array.
SyntaxOccurs (ExpC1, ExpC2) 
Parameters
  • ExpC1 (Required / String) The string, element or array to perform the search on.
  • ExpC2 (Required / String) The string to search for. 
  • RemarksA good example of how to use the new VarType() function and FOR EACH...NEXT command. Also demonstrates how a function can recursively call itself. 
    ReturnsA number representing the total number of times that ExpC2 was found in ExpC1. 
    DependenciesNone. 
    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 -
     
      original source of UDF topic. show actual FunctionLibrary list on Scriptlogic site close top
              printer-friendly mirror of UDF topic on scripting@wanadoo.nl site  




    Copyright © 2003 www.scriptlogic.com & scripting@wanadoo.nl - last updated on 20 May 2003


    Site Meter