ColdFusion Tutorial

Finding Max, Min, Count, Sum and Average values in a string.

I think almost everyone sometimes has use MS Excel and the most used mathematic function like Sum, Average, Count, Max and Min.

This tutorial shows how to do exactly the same with ColdFusion.

At the end of the ColdFusion examples I have put some code from ASP.NET that finding the Max and Min values. I haven't done that to start a discussion. The size of the code tells my feeling.

In those tutorials we are using ColdFusion strings to do everything.

First the Sum functions:

<!--- This is our string of numbers. --->

<cfset test = "23,1,2,3,8">

<!--- Here we count the numbers of integers in the string. --->

<cfset Count = ListLen(test)>

<!--- We start setting sum like zero. --->

<cfset Sum = 0>

<!--- Then we loop to our string. --->

<cfloop index="elements" from ="1" to ="#Count#" step="1">

<!--- Setting sum like old sum plus the element in the string. --->

<cfset Sum = Sum + ListGetAt(test, elements)>

<!--- End of loop. --->

</cfloop>

<!--- Finally we output the sum of our string. --->

<cfoutput>#Sum#</cfoutput>

Average function:

<!--- This is our string of numbers. --->

<cfset test = "23,1,2,3,8">

<!--- We count the numbers of integers in the string. --->

<cfset Count = ListLen(test)>

<!--- Set the start sum to zero. --->

<cfset Sum = 0>

<!--- Loop our string. --->

<cfloop index="elements" from ="1" to ="#Count#" step="1">

<!--- Setting sum like old sum plus the element in the string. --->

<cfset Sum = Sum + ListGetAt(test, elements)>

<!--- End of loop. --->

</cfloop>

<!--- Divide the sum with the length of our string.

Notice: the use / instead of \. --->

<cfset Average = #sum# / #Count#>

<!--- Finally we output the sum of our string. --->

<cfoutput>#Average#</cfoutput>

Count function:

<!--- This is our string of numbers. --->

<cfset test = "23,1,2,3,8">

<!--- Count the numbers of integers in the string. --->

<cfset Count = ListLen(test)>

<!--- Then we output the string Count value. --->

<cfoutput>#Count#</cfoutput>

Max function:

<!--- This is our string of numbers. --->

<cfset test = "23,1,2,3,8">

<!--- Then we making a new list and put the sorted list in it.

Notice: The use of desc function. This will give the biggest number as number one in our new list. --->

<cfset newList = ListSort("#test#","numeric","desc")>

<!--- We find the Max value using ListGetAt position one. --->

<cfset Max = ListGetAt("#newlist#","1")>

<!--- Then we output the string Max value. --->

<cfoutput>#Max#</cfoutput>

Min function:

<!--- This is our string of numbers. --->

<cfset test = "23,1,2,3,8">

<!--- Instead of desc we use asc to get the smallest number first. --->

<cfset newList = ListSort("#test#","numeric","asc")>

<!--- We find the Min value using ListGetAt position one. --->

<cfset Min = ListGetAt("#newlist#","1")>

<!--- Then we output the string Min value. --->

<cfoutput>#Min#</cfoutput>

 

The ASP.NET version finding Max and Min value:

<%@ Page Language="VB" Debug="true" %>

<script language="VB" runat="server">

Sub Page_Load(sender as Object, e as EventArgs)

Const strDefaults As String = "23,1,2,3,8"

Dim strValues As String

Dim arrStrValues() As String

Dim arrIntValues() As Integer

Dim I As Integer

' Retrieve values passed in.

strValues = Request.QueryString("values")

' If nothing is passed in, use our sample data.

If strValues = "" Then

strValues = strDefaults

End If

' Our form is not a server-side form so we need to

' re-populate the text box with the passed in values.

values.Value = strValues

' Create a array of strings from the input.

arrStrValues = Split(strValues, ",")

' Size our new array of integers.

ReDim arrIntValues(arrStrValues.GetUpperBound(0))

' Transfer data from our string array to our int array.

' Otherwise data would be sorted by text so that 100

' would be less then 90 because 1 is smaller then 9.

For I = LBound(arrStrValues) To UBound(arrStrValues)

If IsNumeric(arrStrValues(I)) Then

arrIntValues(I) = CInt(arrStrValues(I))

End If

Next

' Pass integer arrays to the sorting functions.

litMin.Text = Min(arrIntValues)

litMax.Text = Max(arrIntValues)

End Sub

'* Min ******************************************************

' Finds and returns the lowest value in an array of numbers.

'************************************************************

Function Min(ByVal aNumberArray() As Integer) As Integer

' Sort our array.

Array.Sort(aNumberArray)

' The first element is the smallest. Return it.

Min = aNumberArray(aNumberArray.GetLowerBound(0))

End Function

'* Max ******************************************************

'Finds and returns the highest value in an array of numbers.

'************************************************************

Function Max(ByVal aNumberArray() As Integer) As Integer

' Sort our array.

Array.Sort(aNumberArray)

' The last element is the smallest. Return it.

Max = aNumberArray(aNumberArray.GetUpperBound(0))

End Function

</script>

<html>

<head>

<title>ASP.NET Min / Max Functions Sample</title>

</head>

<body>

<p>

<strong>

Min = <asp:Literal id="litMin" runat="server" /><br />

Max = <asp:Literal id="litMax" runat="server" />

</strong>

</p>

<form action="min-max.aspx" method="get">

<input type="text" id="values" name="values" value="" size="30" runat="server" />

<input type="submit">

</form>

<hr />

</body>

</html>

ColdFusion Tutorial test

Finding Max, Min, Count, Sum and Average values in a string.

No test is available or implemented for this ColdFusion Tutorial.

No User Comments.

No User Comments, be the first one to write your comments?

Add your comments

Your Name*:
Your Email:
Site URL:
Site Name:
Comemt Title*:
Your Comment*:
Key Phrase*:
qG3
Your IP Address: 38.103.63.62
 

ScandicWeb is own by Scandic Systems LTD [UK] Company No. 5984000. All other trademarks and copyrights are the property of their respective holders.

[Home [Site Map [Privacy Statement [Site Feedback] [About ScandicWeb]


[ColdFusion [Flash [Search Engine Optimization [Graphics [Cascading Style Sheets (CSS) [JavaScript (JS) [Software]

Partner sites: ScandicSoft


Server time: 19. November 2008 Wednesday