I am trying to evaluate two dynamically assigned variables; then attempting to assign a dynamic variable based on the outcome.
I have much longer code where this works, but I am trying to consolidate.
Here is what I have for the consolidated code:
<cfloop list="dog,cat,pig,horse" index="i">
<cfif (#trim([i]_FMCTotal)# /2) GT #trim([i]_FMC)#>
<cfset #i#colorCode = 'red'>
<cfelseif (#trim([i]_FMCTotal)# /1.5) GT #trim([i]_FMC)#>
<cfset #i#colorCode = 'yellow'>
<cfelse>
<cfset #i#colorCode = 'green'>
</cfif>
</cfloop>
Not much of the code you have posted would get through the parser as you have a few invalid CFML constructs such as
[i]_FMCTotalwhich is not a valid variable name. Also your use of the hashes is not required within the conditional statements, the hashes are used for output.To answer the question there is
evaluate()but a far more readable, secure and maintainable solution would be to leverage a struct for the colour codes.The result of this would be a data structure from which you can easily access any of the items value in subsequent code
If you don’t have access to rework the _FMC variables you could evaluate those
evaluate('#i#_FMCTotal')but it would be far cleaner to use a collection for these also as in my example.