Help - Search - Members - Calendar
Full Version: Sending list to UDF
W3Schools Forum > Server Scripting > ColdFusion
vchris
I have a table where I need to calculate the totals and subtotals. The way it is already done is adding the rows in the table straight from the query and then displaying the subtotal/total. Except now I set the isNull value to -1 instead of 0. Anyway I created a UDF where it will take a list as a parameter and add them if they are not -1 and return the answer. If I do something like #calculate_total("1,2,3,4")# it works great. When I try to use variables instead of values that's when it doesn't work.

CODE
<!--- Function to calculate totals --->
<cffunction name="calculate_total" returntype="numeric">
    <cfargument name="mynum" type="any" />
    <cfset total = 0>
    <!--- <cfset mynum = "1,2,3,4"> --->
    <cfoutput>Listlen = #listlen(mynum)#</cfoutput>
    
    <cfloop list="#mynum#" index="i">
        <cfset total = total + i>
    </cfloop>
    
    <cfreturn total>
</cffunction>
...
#numberformat_en(calculate_total(release.airsta_v, release.airsto_v, release.airfug_v, release.airspi_v, release.airoth_v))#
...

listlen of mynum is 1 and value -1.
Skemcin
you might try breaking up your output so that you do not have too many nested functions. you might need an evaluate function call in there also, like this:
CODE
<cfset vchris="#release.airsta_v#,#release.airsto_v#,#release.airfug_v#,#release.airspi_v#,
#release.airoth_v#">
<cfset vchris=calculate_total(vchris)>
<cfset vchris=numberformat_en(vchris)>
<cfoutput>
#vchris#
</cfoutput>


You might need:
CODE
<cfset vchris=calculate_total(evaluate(vchris))> in place of the second <cfset> statement.


Of course, once you get this worked out, you might make a UDF just for this purpose as well - one UDF can call another - so you don't have to repeat all the work each place/time you want the output.

Hope that helps.
vchris
Seems to work pretty well. Thanks!

But do you have any idea why my UDF didn't get the full list?
Skemcin
Well, I'll try to explain.

First off, I think you did exactly what I would (and still) do which is make it work with a hard coded value and then say, hey all I gotta do now is substitute my value. Good approach.

However, (and this is not bad) if whatever you did is already inside a # sign, then your variable is not gonna really been seen as that if its nested in an function. ColdFusion looks at the out # sign to instantiate the function and then the function is just literally translated since it doesn't know any better. That's when the "evaluate" (or eval if you want to save a few letters typing) function comes into play. It is used (most commonly) as a nested function to basically identify that what it contains is to be processed again by ColdFusion and to not be taken literally. Essentially, (in this case) it says, this is a variable now go get the value.

Other uses of "evaluate" might be in a calculation of a variable and a number, like this:
CODE
#dateformat(evaluate(now() - 3),"mm/dd/yyyy")#

Here, ColdFusion needs to be told that "now()" is to be "evaluated" or processed as a variable before it can carry out the dateformat function.

Hope that helps to explain it and doesn't confuse you more.
vchris
So you're saying that if I would've used something like #numberformat_en(calculate_total(evaluate(release.airsta_v), evaluate(release.airsto_v), evaluate(release.airfug_v), evaluate(release.airspi_v), evaluate(release.airoth_v)))#, it would've worked?
Skemcin
QUOTE (vchris @ Aug 7 2007, 02:32 PM) *
So you're saying that if I would've used something like #numberformat_en(calculate_total(evaluate(release.airsta_v), evaluate(release.airsto_v), evaluate(release.airfug_v), evaluate(release.airspi_v), evaluate(release.airoth_v)))#, it would've worked?

yes (conceptually) I'd have to actually try it. And to reduce your typing, you could have changed "evaluate" to "eval"
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.