ArraySum

Note:

You can also set the maximum thread count in ColdFusion Administrator. Click Server Settings > Settings and specify the number of threads in Default Maximum Thread Count For Parallel Functions.

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<=100000;i++){ 
if(i === 4500) 
arr[i]=0; 
else 
 
arr[i]=i*2; 
} 
 
sum= arr.map(function(item){ 
return item/2; 
},true,20).sum(false,true,10) 
 
writeoutput(sum & "<br>"); 
 
 
sum = arraysum(array=arr,ignoreempty=true,parallel=true,maxthreadcount=10) 
 
writeoutput(sum & "<br>"); 
 
sum1 = arraysum(arr,false,true,10) 
writeoutput(sum1 & "<br>"); 
</cfscript>
Adobe logo

Sign in to your account