Do you know of any way to dynamically create the condition for an if statement? I think the best way to explain this is with an example. I'm looking do to something along the lines of the following
<cfset firstElement = "hello" />
<cfset operator = "eq" />
<cfset secondElement = firstElement />
<cfif firstElement operator secondElement></cfif>
I've tried various methods and variations using cfscript, iif(), and combinations of evaluate() and de(). Additionally I've searched the usual suspects for answers. (coldfusion, cf docs, cfwack, cf advanced, etc...) I realize there are longhand ways around this such as using a cfswitch to evaluate the value of operator, but I'm hoping to be able to simply support all of CF's native operators without having to essentially write the same code for each operator choice.
Most likely you just had a typo when you tried evaluate(), and frankly, evaluate can be a bit confusing at times. This code sample worked for me:
<cfset first = "ray"> <cfset second = "ray"> <cfset op = "eq"> <cfoutput>#evaluate("first #op# second")#</cfoutput>
Switching second to "paris hilton" correctly returned a false value. Note though that in general, when I see evaluate I get a bit uneasy. Evaluate is not as slow as it used to be when you use ColdFusion 8. But it always strikes me as kind of a dirty function. Like - should I really be using this? I'm not telling folks to not use it (like I used to). I will say that most of the time when I do see it - it isn't necessary.
One of the classic examples was - I have a variable that points to another variable. How do I get the value? They would then do this:
<cfset variable = "name"> <cfset value = evaluate(variable)>
But with scopes, this is a lot simpler, and easier to read I think:
<cfset value = variables[variable]>