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

  1. /*
  2.  * File......: ASum.Prg
  3.  * Author....: David Husnian
  4.  * Date......: $Date:   15 Aug 1991 23:02:48  $
  5.  * Revision..: $Revision:   1.2  $
  6.  * Log file..: $Logfile:   E:/nanfor/src/asum.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/asum.prv  $
  15.  * 
  16.  *    Rev 1.2   15 Aug 1991 23:02:48   GLENN
  17.  * Forest Belt proofread/edited/cleaned up doc
  18.  * 
  19.  *    Rev 1.1   14 Jun 1991 19:50:56   GLENN
  20.  * Minor edit to file header
  21.  * 
  22.  *    Rev 1.0   01 Apr 1991 01:00:36   GLENN
  23.  * Nanforum Toolkit
  24.  *
  25.  */
  26.  
  27.  
  28. /*  $DOC$
  29.  *  $FUNCNAME$
  30.  *     FT_ASUM()
  31.  *  $CATEGORY$
  32.  *     Array
  33.  *  $ONELINER$
  34.  *     Sum the elements of an array
  35.  *  $SYNTAX$
  36.  *     FT_ASUM( <aArray> [, <nStartIndex> [, <nEndIndex> ] ] ) -> nSum
  37.  *  $ARGUMENTS$
  38.  *     <aArray> is the array containing the elements to be summed.
  39.  *
  40.  *     <nStartIndex> is the first array item to include,
  41.  *     defaults to first element.
  42.  *
  43.  *     <nEndIndex> is the last array element to include,
  44.  *     defaults to all elements.
  45.  *  $RETURNS$
  46.  *     The sum of the elements of the array or the lengths of the elements.
  47.  *  $DESCRIPTION$
  48.  *     This function is to sum the elements of a numeric array or to sum the
  49.  *     lengths of a character array.
  50.  *  $EXAMPLES$
  51.  *
  52.  *     FT_ASUM(aSubTotals)               // Sum the Entire Array
  53.  *
  54.  *     FT_ASUM(aSubTotals, 5)            // Sum from the 5th Element On
  55.  *
  56.  *     FT_ASUM(aSubTotals, , 10)         // Sum the 1st 10 Elements
  57.  *
  58.  *     FT_ASUM(aSubTotals, 5, 10)        // Sum Elements 5-10
  59.  *  $END$
  60.  */
  61.  
  62. #define CASE_AT(x,y,z)               z[AT(x,y)+1]
  63. #define FORCE_BETWEEN(x,y,z)         (y := MAX(MIN(y,z),x))
  64. #define IS_CHAR(x)                   (VALTYPE(x) == "C")
  65.  
  66. #command    DEFAULT <Param1> TO <Def1> [, <ParamN> TO <DefN> ] ;
  67.             => ;
  68.             <Param1> := IF(<Param1> == NIL,<Def1>,<Param1>) ;
  69.          [; <ParamN> := IF(<ParamN> == NIL,<DefN>,<ParamN>)]
  70.  
  71.  
  72. FUNCTION FT_ASUM(aArray, nStartIndex, nEndIndex)
  73.  
  74.    LOCAL nSumTotal := 0                 // Array Sum
  75.  
  76.    DEFAULT nStartIndex TO 1, ;
  77.            nEndIndex   TO LEN(aArray)
  78.                                         // Make Sure Bounds are in Range
  79.    FORCE_BETWEEN(1, nEndIndex,   LEN(aArray))
  80.    FORCE_BETWEEN(1, nStartIndex, nEndIndex)
  81.  
  82.    AEVAL(aArray, ;
  83.          { | xElement | ;
  84.            nSumTotal += ;
  85.               CASE_AT(VALTYPE(xElement), "NC", ;
  86.                       { 0, xElement, ;
  87.                            IF(IS_CHAR(xElement),LEN(xElement),0) }) }, ;
  88.          nStartIndex, nEndIndex - nStartIndex + 1)
  89.  
  90.    RETURN (nSumTotal)                   // FT_ASum
  91.