Solved

looping with calculations

Posted on 2014-03-25
5
171 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem in SELECT Statement 5 77
Build PDF in coldfusion 2 63
RETURN INFORMATION FROM COLDFSION CFC 1 63
populate select menu with array from another select menu 3 65
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 …
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 …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

21 Experts available now in Live!

Get 1:1 Help Now