Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with cfloop and cfset

Status
Not open for further replies.

jgroove

Programmer
Jul 9, 2001
43
US
I am trying to create a list of variables named
Trainer1
Trainer2
Trainer3
Trainer4
etc....

Code:
<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;#ListLen(form.selTrainerID)#&quot;>
   <cfset Trainer#i#=ListGetAt(form.selTrainerID,i)>
</cfloop>





 
You were close... all you need is quotes around &quot;Trainer#i#&quot;, the dynamic variable you are tying to set.

<cfloop index=&quot;i&quot; from=&quot;1&quot; to=&quot;#ListLen(form.selTrainerID)#&quot;>
<cfset &quot;Trainer#i#&quot; = ListGetAt(form.selTrainerID,i)>
</cfloop> - tleish
 
Dynamically named variables are convenient, but it is not a good practice to use them. Use a structure instead. The key to the structure would the name that you would have named the variable. The reason to shun dynamically named variables is that they are bulky and clumsy. In your example, it's not so bad, because you use the variable &quot;i&quot;, the value of which you know to be a valid character for a variable name. but, if you grab some data out of a database and try to use that data as part of variable name, you may have problems. The key of a structure is much more lenient; it can include spaces and punctuation marks. Also, with dynamically named variables, you can't easily count all the &quot;trainer&quot; variables, or sort them or pass them to the next page by serializing them. It just a lot easier to get into the habit of using structures.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top