• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Variables in Variables

New Here ,
May 28, 2008 May 28, 2008

Copy link to clipboard

Copied

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?

surprised

Thank you,

John
TOPICS
Advanced techniques

Views

434

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 28, 2008 May 28, 2008

Copy link to clipboard

Copied

This is screaming for a structure or array or maybe an array of
structures possible even an component|object.

But if you insist on doing it this way, array notation is what you are
looking for.

<cfset variables['date' & i] = dateFormat(dateAdd('M',i,now()))>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 28, 2008 May 28, 2008

Copy link to clipboard

Copied

In addition to what Ian said, Cold Fusion is probably letting you get away with a bad programming practice. DateFormat returns a string, not a date. Plus, the fact that you are creating odbcdates inside a loop suggestst that you are doing something inefficient elsewhere in your app.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 29, 2008 May 29, 2008

Copy link to clipboard

Copied

Alternatively:

<CFSET "VARIABLES.EndDate#i#" = "#Month1#/#numDays1#/#Year1#">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 29, 2008 May 29, 2008

Copy link to clipboard

Copied

LATEST


Thanks for all the replies folks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation