Parameter
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.
Calculates the average of the values in an array.
Number. If the array parameter value is an empty array, returns zero.
arrayAvg(array [, parallel] [, maxThreadCount])
ColdFusion (2021 release): Introduced the following parameters:
ArraySum; Basic array techniques in the Developing ColdFusion Applications
|
Parameter |
Description |
|
array |
(Required) The array containing the values. |
|
parallel |
(Optional) True if you want to enable parallel programming. |
|
maxThreadCount |
(Optional) 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. |
The following example uses the ArrayAvg function to get the average value of an array.
<cfscript>
myArray=[1,2,3,4,5,5.5,6,7,8,9];
writeOutput(ArrayAvg(myArray)); // returns 5.05
</cfscript>
5.05
<cfscript>
for(i=1;i<=1001;i++){
arr[i] = i/2;
if (i === 1000)
arr[i] = i;
}
function callback(item,index){
item = item + 2;
return item;
}
writeoutput("with third param as false , i.e no parallel:" & arr.map(callback).avg(false,2) & "<br>")
try{
writeoutput("with negative thread ,should throw exception:" & "<br>")
arr.map(callback).avg(true,-5)
}
catch(any e){
writedump("Type: " & e.type & " Message:" & e.message )
}
writeoutput("<br>with 10 threads :" & arr.map(callback).avg(true,10) & "<br>")
arr =arraymap(array=arr,callback=callback)
writeoutput("with 20 threads : " & arrayavg(array=arr,parallel=true,maxthreadcount=20) & "<br>")
arr=arraymap(array=arr,callback=callback)
writeoutput("with 50 threads :" & arrayavg(array=arr,parallel=true,maxthreadcount=50) & "<br>")
try{
writeoutput("with more than 50 thread ,should throw exception" & "<br>")
arr.map(callback).avg(true,100);
}
catch(any e){
writedump("Type: " & e.type & " Message:" & e.message )
}
arr =arraymap(array=arr,callback=callback)
writeoutput("<br> with no threads specified, will take thread count from cf admin: " & arrayavg(array=arr,parallel=true) & "<br>")
</cfscript>
Sign in to your account