?
Solved

looping with calculations

Posted on 2014-03-25
5
Medium Priority
?
175 Views
Last Modified: 2014-03-25
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

0
Comment
Question by:earwig75
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 4

Expert Comment

by:Rodrigo Munera
ID: 39953310
<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

0
 

Author Comment

by:earwig75
ID: 39953320
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.
0
 
LVL 4

Assisted Solution

by:Rodrigo Munera
Rodrigo Munera earned 1000 total points
ID: 39953330
An easier way of encapsulating your business logic (e.g. your calculations) would be to create another function in your cfc to handle those calculations and pass the query to it. Something like this:

<cfset myCFC = createObject("component","myCFCFile")>
<cfloop...>
    <cfset myQueryObject = myCFC.getQueryObject(namedArg1="value",namedArg2="value2")>
    <cfset myBusinessRules = myCFC.getCalculationResults(query=myQueryObject)>
    <cfoutput>
        #myBusinessRules.something# is the result of #myBusinessRules.somethingElse#
    </cfoutput>
</cfloop>

Open in new window

0
 
LVL 4

Assisted Solution

by:Rodrigo Munera
Rodrigo Munera earned 1000 total points
ID: 39953351
In your function you would set your variables and checks.

<cffunction name="getCalculationResults">
    <cfargument name="myQuery" required="true">
    <cfset var something = 0>
    <cfset var somethingElse = 0>
    <cfset var stReturn = structNew()>
    <cfif myQuery.datecr NEQ 0>
        Do your division
        <cfset stReturn.something = 1234>
        <cfset stReturn.somethingElse = 5555>
    <cfelse>
        Don't do your division.
        <cfset stReturn.something = 4321>
        <cfset stReturn.somethingElse = 4444>
    </cfif>
    <cfreturn stReturn>
</cffunction>

Open in new window

0
 
LVL 52

Accepted Solution

by:
_agx_ earned 1000 total points
ID: 39953399
(Edit)

> 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?

Yes. You could initialize it to zero, then do the division only if the divisor is > 0.

        <cfloop >
                <!--- initialize it on each loop --->
                 <cfset somethingelse = 0 />

                <!--- do division only if divisor is valid --->
              <cfif something neq 0>
                     <cfset somethingelse = ... do division ..../>
               </cfif>

                       ... At this point, "somethingelse" always has a value
         </cfloop>

Some people prefer that style, others prefer to cfif/cfelse like in your earlier example.  

              <cfif something neq 0>
                        <cfset somethingelse = ... do division ..../>
                 <cfelse>
                        <cfset somethingelse = 0 />
               </cfif>

Since both ensure the variable always exists, it's a matter of personal style.

Another option is to create a custom divide function. Simply return a default value if the divisor is 0.

        <cffunction name="divideCustom" returntype="numeric" ...>
             <cfargument name="numerator" type="numeric" required="true">
             <cfargument name="divisor" type="numeric" required="true">
             <cfargument name="defaultValue" type="numeric" default="0">

             <cfif arguments.divisor neq 0>
                   <cfreturn arguments.numerator / arguments.divisor >
             </cfif>

             <cfreturn arguments.defaultValue />
        </cffunction>

Then use that function within your loop:

        <cfloop ...>
            <cfset somethingelse = divideCustom( someValue, someOtherValue)>
        </cfloop>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question