Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to sum up numbers in a list

Status
Not open for further replies.

olmos

Technical User
Oct 25, 2000
135
US
I have a list and I need to add them all up to get the total.
How do I do this?

This is my list of values:
<CFSETid_list = ListAppend(id, count)>
<TD align = &quot;center&quot;> #id_list# &nbsp; </TD>

Thanks in advance,
Olmos.
 
Here's one way

[COLOR=666666]<!--- Set up Variables --->[/color]
<cfscript>
id = &quot;10,2,356,65&quot;;
count = 81;
</cfscript>

=== START CODE EXAMPLE ===
<CFSET id_list = ListAppend(id, count)>
[COLOR=666666]<!--- Change the delimiters from &quot;,&quot; to &quot;+&quot; --->[/color]
<CFSET id_list = ListChangeDelims(variables.id_list, &quot;+&quot;)>
[COLOR=666666]<!--- Total the items in the list --->[/color]
<CFSET id_total = Evaluate(variables.id_list)>

<cfoutput>
[COLOR=008080]<TD align = &quot;center&quot;>[/color]#variables.id_total#[COLOR=008080]</TD>[/color]
</cfoutput>
=== END CODE EXAMPLE ===


or you could do it all in one line:

=== START CODE EXAMPLE ===
<cfoutput>
[COLOR=008080]<TD align = &quot;center&quot;>[/color]#Evaluate(ListChangeDelims(ListAppend(id, count), &quot;+&quot;))#[COLOR=008080]</TD>[/color]
</cfoutput>
=== END CODE EXAMPLE === - tleish
 
You could convert the list to an array and use ArraySum to get the total:

<cfset lstID = &quot;10,2,356,65&quot;>
<cfset arrId = ListToArray(lstId, &quot;,&quot;)>
<cfoutput>#ArraySum(arrId)#</cfoutput>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top