Jump to content

Sending list to UDF


Recommended Posts

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.

<!--- 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.

Link to post
Share on other sites

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:

<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:

<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.

Link to post
Share on other sites

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:

#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.

Link to post
Share on other sites

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?

Link to post
Share on other sites
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"
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...