Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

looping with calculations

Posted on 2014-03-25
5
Medium Priority
?
176 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

609 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