Solved

looping with calculations

Posted on 2014-03-25
5
170 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
Comment Utility
<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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
(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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now