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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variables in Variables

Status
Not open for further replies.

OneTrickPony

Programmer
May 27, 2008
3
US
I have some code that creates variables for each of the next 12 months. This is an example for the second month.

<CFSET VARIABLES.Date1 = DateFormat(DateAdd("M", 1, Now()))>
<CFSET VARIABLES.numDays1 = DaysInMonth(Date1)>
<CFSET VARIABLES.Month1 = Month(Date1)>
<CFSET VARIABLES.MonthString1 = MonthAsString(Month1)>
<CFSET VARIABLES.MonthStrg1 = Left(MonthString1, 3)>
<CFSET VARIABLES.Year1 = Year(Date1)>
<CFSET VARIABLES.BegDate1 = "#Month1#/1/#Year1#">
<CFSET VARIABLES.BegDate1 = CreateODBCDate(BegDate1)>
<CFSET VARIABLES.EndDate1 = "#Month1#/#numDays1#/#Year1#">
<CFSET VARIABLES.EndDate1 = CreateODBCDate(EndDate1)>

Now I could write this all out and replace the integer "1" with "2", "3" etc. over and over but it makes sense to save typing and put this in a loop.

Problem is I can't seem to figure out how.

<CFLOOP FROM="1" TO="11" INDEX="i">
<CFSET VARIABLES.Date#i# = DateFormat(DateAdd("M", #i#, Now()))> etc. ???
</CFLOOP>

This line looks particulary tricky:
<CFSET VARIABLES.EndDate1 = "#Month1#/#numDays1#/#Year1#">

How do you put a variable in a variable? Something to do with evaluate?

:-0

Thank you,

John
 
Hi there,

yes you could use evaluate to solve your problem with a slight change to your code:

Code:
<CFLOOP FROM="1" TO="11" INDEX="i">
<CFSCRIPT>
   evaluate("VARIABLES.Date#i# = DateFormat(DateAdd('M', #i#, Now()))");
</CFSCRIPT>
</CFLOOP>
hope this helps,

Pete

 
Excellent - Thanks.

I also found out I could use array notation:

<CFSET VARIABLES['Date' & i] = DateFormat(DateAdd("M", i, Now()))>

I still have a lot of optimization to do but I'm on my way no.

Thank you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top