Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

3.9. Array Aggregate Functions

You want to perform aggregate functions on array elements.

Technique

Use ColdFusion array aggregate functions to perform a function on all elements within an array.









<!---Create aArray array and set array values--->
<cfset numList = "0,5,22,7,31">
<cfset aArray = ListToArray(numList)>
aArray aggregate functions
<hr>
<cfoutput>

<!---Perform aggregate functions on aArray and display results--->

Maximum Value: #ArrayMax(aArray)#<br>

Minimum Value: #ArrayMin(aArray)#<br>

Sum: #ArraySum(aArray)#<br>

Average: #ArrayAvg(aArray)#
</cfoutput>

Comments

Aggregate functions are functions executed on multiple values—array aggregate functions are functions executed on each array element value.

Table 3.1 defines the four functions available in ColdFusion MX.

Table 3.1 CFMX Array Aggregate Functions

Function

Return Value

ArrayMax(array)

Returns the maximum numeric value in array.

ArrayMin(array)

Returns the smallest numeric value in array.

ArrayAvg(array)

Returns the numeric average of all values in array.

ArraySum(array)

Returns the numeric sum of all values in array.


Two exceptions of note when dealing with array aggregate functions are as follows:

  • If there is a non-numeric value in an array passed into an aggregate function, ColdFusion throws a runtime exception.

  • If the array passed into an aggregate function is empty, each of the functions returns a 0.

Note that ColdFusionMX returns 0 when an empty array is passed into an array aggregate function. ColdFusion 5 returned infinity.

  • + Share This
  • 🔖 Save To Your Account