[thelist] ColdFusion function for dynamic variable name?

.jeff jeff at members.evolt.org
Tue Jul 10 13:25:21 CDT 2001


matt,

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: From: Plunkett, Matt
:
: Is there a function in ColdFusion that
: will let me construct variable names from
: strings and then use them as variables?
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sure, but it depends on what you want to do though.  do you want to
dynamically create a variable that you can use to set values to later?  or,
do you want to dynamically create a variable name and evaluate it to get
it's value?

if the former, use the SetVariable() function to create the variable foo
with a value of bar.

<cfset temp = SetVariable('foo', 'bar')>

if the latter, use the Evaluate() function to concatenate variables and/or
strings to create a variable name and evaluate that variable to its value:

<cfset foo = 'bar'>
<cfset o = "o">
Evaluate('fo' & o)

the above will evaluate to the value bar.

:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: I have two sets of form fields, one hidden
: and one from various inputs.  The hidden
: ones are all called oldField1, oldField2,
: oldField3, and so on and the others are
: called requestField1, requestField2, and
: so on.
:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

since you're using form fields though, you can access them via the form
struct using bracket notation, as opposed to the dot notation you're used
to.  this allows you to use strings and variables within the brackets to
create the field name.

<cfset listFields = "Field1, Field2, Field3, Field4">

<cfloop list="#listFields#" index="field">
  <cfif form["old" & field] IS NOT form["request" & field]>
    <!--- Do whatever. --->
  </cfif>
</cfloop>

notice i removed the hashes from your <cfif> tag.  inside a tag that's
expecting variables like this the hashes are unnecessary.

good luck,

.jeff

http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/






More information about the thelist mailing list