Link to home
Start Free TrialLog in
Avatar of earwig75
earwig75

asked on

looping with calculations

I have a query in a .cfc that accepts arguments and then returns some values. I am looping over the cfinvoke and sending a bunch of different arguments to the query and displaying the results in a table with some calculations based on those results. I am wondering; do I need to set each variable to zero (0) with a cfparam like I did 2 below? would I set each variable to zero each time in the loop before the group of calculations begins to avoid it from keeping the value from the previous loop? The 'neq 0' was to avoid division by zero but I put the others in there as well. (This is new to me). Below is a small example of what I'm doing in the loop section, thank you!:

<cfloop list="#myList#" index="theNames">
<cfinvoke component="... returnvariable="camebck">
	<cfinvokeargument name="theName" value="#theNames#">
</cfinvoke>
<cfoutput>
<cfparam name="something" default="0">
<cfparam name="somethingelse" default="0">

<cfif camebck.datecr neq 0>
	<cfset countstayed = #camebck.datecr# - (#camebck.closedneutral# + #camebck.badones#) />
	<cfset something = #camebck.closedbad# + #camebck.closedgood# />
	     <cfif something neq 0>
	     	<cfset somethingelse = #camebck.closedbad# / #something# * 100 />
	     <cfelse>
	     	<cfset somethingelse = 0 />
	     </cfif>
	<cfset maybebadones = #countstayed# * #somethingelse# / 100 />
	<cfset amounts = maybebadones* 204 />
<cfelse>
	<cfset countstayed = 0 />
	<cfset something = 0 />
	<cfset somethingelse = 0 />
	<cfset maybebadones = 0 />
	<cfset amounts= 0 />

<tr><td>#theName#</td><td>#countstayed#</td>...............
</cfoutput>
</cfloop>

Open in new window

Avatar of Rodrigo Munera
Rodrigo Munera
Flag of United States of America image

<cfparam> only fires if the variable name in the name argument does not exist. cfparam basically does this:

<cfparam name="myVar" default="0">

is the same as:

<cfif NOT isDefined("myVar")>
        <cfset myVar = 0>
</cfif>

Open in new window

So, each iteration will contain the value of myVar after it has been set in the previous iteration.

To ensure the value of myVar is 0 at the beginning of each loop iteration, just replace <cfparam> with <cfset>:

    <cfloop...>
        <cfset myVar = 0>
    </cfloop>

Open in new window

Avatar of earwig75
earwig75

ASKER

If I set each var to zero, would I only have to put the others in a CFIF, if I am dividing, to prevent division by zero? Thanks.
SOLUTION
Avatar of Rodrigo Munera
Rodrigo Munera
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial