home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / nfsrc21.zip / ANOMATCH.PRG < prev    next >
Text File  |  1991-08-16  |  3KB  |  95 lines

  1. /*
  2.  * File......: ANoMatch.Prg
  3.  * Author....: David Husnian
  4.  * Date......: $Date:   15 Aug 1991 23:02:44  $
  5.  * Revision..: $Revision:   1.2  $
  6.  * Log file..: $Logfile:   E:/nanfor/src/anomatch.prv  $
  7.  * 
  8.  * This is an original work by David Husnian and is placed in the
  9.  * public domain.
  10.  *
  11.  * Modification history:
  12.  * ---------------------
  13.  *
  14.  * $Log:   E:/nanfor/src/anomatch.prv  $
  15.  * 
  16.  *    Rev 1.2   15 Aug 1991 23:02:44   GLENN
  17.  * Forest Belt proofread/edited/cleaned up doc
  18.  * 
  19.  *    Rev 1.1   14 Jun 1991 19:50:52   GLENN
  20.  * Minor edit to file header
  21.  * 
  22.  *    Rev 1.0   01 Apr 1991 01:00:32   GLENN
  23.  * Nanforum Toolkit
  24.  *
  25.  */
  26.  
  27.  
  28. /*  $DOC$
  29.  *  $FUNCNAME$
  30.  *     FT_ANOMATCHES()
  31.  *  $CATEGORY$
  32.  *     Array
  33.  *  $ONELINER$
  34.  *     Find the number of array elements meeting a condition
  35.  *  $SYNTAX$
  36.  *     FT_ANOMATCHES( <aArray>, <bCompareBlock> ;
  37.  *                    [, <nStartIndex> [, <nEndIndex> ] ] ) -> nNoOfMatches
  38.  *  $ARGUMENTS$
  39.  *     <aArray> is the array to be searched
  40.  *
  41.  *     <bCompareBlock> is a code block containing the expression for
  42.  *     the array elements to be tested with.  Each element is passed
  43.  *     as a parameter to the block.  If the block returns .T., the
  44.  *     number of matches will be incremented by one.
  45.  *
  46.  *     <nStartIndex> is the first array item to include in the search,
  47.  *     defaults to first element.
  48.  *
  49.  *     <nEndIndex> is the last array element to include in the search,
  50.  *     defaults to all elements.
  51.  *  $RETURNS$
  52.  *     The number of elements that cause the code block to return .T.
  53.  *  $DESCRIPTION$
  54.  *     This function returns the number of array elements that, when passed
  55.  *     to the supplied code block, cause that code block to return a .T. value.
  56.  *  $EXAMPLES$
  57.  *     // Search the Entire Array
  58.  *     FT_ANOMATCHES(aTries, { | x | x <= 100 } )
  59.  *
  60.  *     // Search from the 5th Element On
  61.  *     FT_ANOMATCHES(aCodes, { | x | UPPER(x) == cCurrentCode }, 5)
  62.  *
  63.  *     // Search the 1st 10 Elements
  64.  *     FT_ANOMATCHES(aDates, { | x | IS_BETWEEN(DATE()-7,x,DATE() + 7) }, 10)
  65.  *
  66.  *     // Search Elements 5-10
  67.  *     FT_ANOMATCHES(aNames, { | x | x <= cLastGoodName }, 5, 10)
  68.  *  $END$
  69.  */
  70.  
  71. #define FORCE_BETWEEN(x,y,z)         (y := MAX(MIN(y,z),x))
  72.  
  73. #command    DEFAULT <Param1> TO <Def1> [, <ParamN> TO <DefN> ] ;
  74.             => ;
  75.             <Param1> := IF(<Param1> == NIL,<Def1>,<Param1>) ;
  76.          [; <ParamN> := IF(<ParamN> == NIL,<DefN>,<ParamN>)]
  77.  
  78. FUNCTION FT_ANOMATCHES(aArray, bCompareBlock, nStartIndex, nEndIndex)
  79.  
  80.    LOCAL nNoOfMatches := 0              // Number of Matches Found
  81.  
  82.    DEFAULT nStartIndex TO 1, ;
  83.            nEndIndex   TO LEN(aArray)
  84.  
  85.                                         // Make Sure Bounds are in Range
  86.    FORCE_BETWEEN(1, nEndIndex,   LEN(aArray))
  87.    FORCE_BETWEEN(1, nStartIndex, nEndIndex)
  88.  
  89.    AEVAL(aArray, ;
  90.          { | xElement | ;
  91.            IIF(EVAL(bCompareBlock, xElement), nNoOfMatches++, NIL) }, ;
  92.          nStartIndex, nEndIndex - nStartIndex + 1)
  93.  
  94.    RETURN (nNoOfMatches)                // FT_ANoMatches
  95.