ArraySum

Description

Use this function to calculate the sum of values in an array.

Returns

The sum of values in an array. If the array parameter value is an empty array, returns zero.

Category

Array functionsMathematical functions

Function syntax

arraySum(array [, parallel] [, maxThreadCount])

History

ColdFusion (2021 release): Introduced the following parameters:

  • parallel
  • maxThreadCount

ColdFusion (2018 release): Introduced named parameters.

ColdFusion (2016 release) Update 3 - Added ignoreUndefined parameter.

Parameters

Parameter

Description

array

(Required) Name of an array.

ignoreEmpty

(Optional) Boolean (true/false) value denoting whether to ignore empty (Null or "") values in the array while adding the elements. False by default.

parallel

True if you want to enable parallel programming.

maxThreadCount

The number of threads the function can execute. The number of threads must be between 1-50. If the value exceeds 50, there is an exception.

Example

<cfscript>
       myArray=[921,22,133,40,345,58,-6];
       WriteOutput("Sum of values in myArray is:");
       WriteOutput(ArraySum(myArray) & "|"); // returns sum of values in myArray
       myNewArray=[];
       WriteOutput("Sum of values in myNewArray is:");
       WriteOutput(ArraySum(myNewArray)); //returns zero since myNewArray is empty
</cfscript>

Output

Sum of values in myArray is:1513|Sum of values in myNewArray is:0

Example - using the ignoreEmpty parameter

<cfscript>
       myarray=ArrayNew(1);
       myarray=[-23,56,97,javacast("null",""),"",1,23];
       sum=arraysum(myarray,true); //ignoreEmpty=true
       writeoutput(sum);
</cfscript>

Output

154

Using parallelization

<cfscript> 
    for(i=1;i<=1001;i++){ 
   
  arr[i] = i+25; 
  if (i == 1000) 
   arr[i] = i; 
 } 
    //writeDump(arr) 
    function callback(item,index){ 
        item = item + 2; 
            return item; 
    } 
    t_start=GetTickCount() 
    writeoutput(arr.map(callback).sum() & "<br/>") 
    t_end=GetTickCount() 
    writeoutput("<br>Time taken with no parallel:" &  t_end-t_start) 
 
    t_start=GetTickCount() 
    writeoutput(arr.map(callback).sum(true,50) & "<br/>") 
 t_end=GetTickCount() 
 writeoutput("<br>Time taken with 50 threads:" &  t_end-t_start) 
</cfscript>

 Adobe

Get help faster and easier

New user?

Adobe MAX 2024

Adobe MAX
The Creativity Conference

Oct 14–16 Miami Beach and online

Adobe MAX

The Creativity Conference

Oct 14–16 Miami Beach and online

Adobe MAX 2024

Adobe MAX
The Creativity Conference

Oct 14–16 Miami Beach and online

Adobe MAX

The Creativity Conference

Oct 14–16 Miami Beach and online