Solved

dynamic assignment of variables?

Posted on 2004-09-29
10
135 Views
Last Modified: 2013-12-24
Is this possible I need variables for the amount of weeks between startweeks and endweeks (t01, t02, t03, t04)

<cfloop index="j3" from="#startweek#" to="#endweek#" step="1">
<cfset 't'&j3 = 0>
</cfloop>

john
0
Comment
Question by:jcorbin
  • 4
  • 3
10 Comments
 
LVL 35

Accepted Solution

by:
mrichmon earned 50 total points
ID: 12185450
Like this :

<cfset Variables['t' & j3] = 0>

Or for form variables use :

<cfset Form['t' & j3] = 0>

Or for URL variables or session variables do similarly...
0
 
LVL 7

Expert Comment

by:INSDivision6
ID: 12186630
Something like this using Evaluate():


<html><body>

<!--- Create and assign --->
<cfloop index="i" from="1" to="10">
   <cfset Evaluate("t#NumberFormat(i, '00')#=i")>
</cfloop>

<!--- Test them --->
<cfoutput>t01=#t01#</cfoutput><br>
<cfoutput>t02=#t02#</cfoutput><br>
<cfoutput>t03=#t03#</cfoutput><br>
<cfoutput>t04=#t04#</cfoutput><br>
<cfoutput>t05=#t05#</cfoutput><br>
<cfoutput>t06=#t06#</cfoutput><br>
<cfoutput>t07=#t07#</cfoutput><br>
<cfoutput>t08=#t08#</cfoutput><br>
<cfoutput>t09=#t09#</cfoutput><br>
<cfoutput>t10=#t10#</cfoutput><br>

</body></html>
0
 
LVL 10

Expert Comment

by:Mause
ID: 12187399
Hi there,

I would just do something like this:

<cfloop index="j3" from="#startweek#" to="#endweek#" step="1">
   <cfset 't#j3#' = 0>
</cfloop>

and if you want session variables:
<cfset 'session.t#j3#' = 0>

INSDivision try to avoid 'evaluate' because its slow esspecialy in loops!
You can also use cfdump to see all the variables: <cfdump var="#variables#"> or <cfdump var="#sessions#">

Mause
0
Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

 
LVL 35

Expert Comment

by:mrichmon
ID: 12191531
Mause - you are correct that evaluate is slow, but so is using # the way you showed.

Really the only efficient method is accessing it from teh scope as in my example.
0
 
LVL 10

Expert Comment

by:Mause
ID: 12191943
mrichmon - I just run a test width your code and mine
and indeed yours is faster (in a loop of 1000) but its only 5 to 10ms :)


0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12191975
There is also another advantage.

The scope syntax (the method I showed) works in every situation I have come across so far.

I used to use your method or the evaluate until I found that in some complex situations it actually doesn't work and the only way to actually work is to use the scope syntax.
0
 
LVL 10

Expert Comment

by:Mause
ID: 12192041
mrichmon your code is great and I have to admit that even I use it is some situation
Lets say I only showed an ohter way how to do it

0
 
LVL 35

Expert Comment

by:mrichmon
ID: 12192880
Yep

I wasn't criticizing your way... I used it quite a lot until I found this scope method.

:o)
0

Featured Post

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question