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
Solved

looping with calculations

Posted on 2014-03-25
5
172 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
  • 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 250 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 250 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 250 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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 …
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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