Ike23
asked on
Should I be using an Array or Structure here? I get an error when trying to use cfset with a dynamic variable...
I have the joyous task of migrating a bunch of non-normalized data into a new normalized database and have run into a problem. One of the database fields contains a list string of numbers delimited by the | bar. I have 4 categories that I can optionally set and I am using the <cfswitch><cfcase> method to map the old values to the new values. The problem is that I am trying to dynamically set the variable name inside of a loop and CF gives me an error. Here's my example code:
<!--- SET UP THE 4 CATEGORY VARIABLES WITH DEFAULTS OF ZERO AND OVERRIDE THE VALUES IF MATCHES ARE FOUND --->
<cfparam name="CATEGORY1" default="0" type="numeric">
<cfparam name="CATEGORY2" default="0" type="numeric">
<cfparam name="CATEGORY3" default="0" type="numeric">
<cfparam name="CATEGORY4" default="0" type="numeric">
<cfset catNumber = 0>
<cfloop index="cat" list="#CATLIST#" delimiters="|">
<cfset catNumber = catNumber + 1>
<cfswitch expression="#cat#">
<cfcase value="6"><cfset CATEGORY#cat# = "1"></cfcase>*****HERE'S WHERE THE ERROR OCCURS******
<cfcase value="7"><cfset CATEGORY#cat# = "3"></cfcase>
<cfdefaultcase>
<cfset CATEGORY#cat# = "0">
</cfdefaultcase>
</cfswitch>
Then I do a database insert using the Category1 - 4 variables here.
</cfloop>
Is there a way to do this using an array or structure or am I going about this all wrong? Thanks for any advice!
<!--- SET UP THE 4 CATEGORY VARIABLES WITH DEFAULTS OF ZERO AND OVERRIDE THE VALUES IF MATCHES ARE FOUND --->
<cfparam name="CATEGORY1" default="0" type="numeric">
<cfparam name="CATEGORY2" default="0" type="numeric">
<cfparam name="CATEGORY3" default="0" type="numeric">
<cfparam name="CATEGORY4" default="0" type="numeric">
<cfset catNumber = 0>
<cfloop index="cat" list="#CATLIST#" delimiters="|">
<cfset catNumber = catNumber + 1>
<cfswitch expression="#cat#">
<cfcase value="6"><cfset CATEGORY#cat# = "1"></cfcase>*****HERE'S WHERE THE ERROR OCCURS******
<cfcase value="7"><cfset CATEGORY#cat# = "3"></cfcase>
<cfdefaultcase>
<cfset CATEGORY#cat# = "0">
</cfdefaultcase>
</cfswitch>
Then I do a database insert using the Category1 - 4 variables here.
</cfloop>
Is there a way to do this using an array or structure or am I going about this all wrong? Thanks for any advice!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Looking at your code may be you do not need to do what you are trying to do.
You already running a loop over CAT variables
Now this CAT variable is in cfcase.
So when the cfcase statement is execuated you already know what is the cat because you are writing the speical condition for that.
For example in the line
<cfcase value="6"><cfset CATEGORY#cat# = "1"></cfcase>*****HERE'S WHERE THE ERROR OCCURS******
<cfcase value="7"><cfset CATEGORY#cat# = "3"></cfcase>
you already know that your cat is 6 and 7 . in that case... why not write direct cfset as follows because programmatically this way is more clear and organized.
<cfcase value="6"><cfset CATEGORY6 = "1"></cfcase>
<cfcase value="7"><cfset CATEGORY7= "3"></cfcase>
Also if you using integer may be you do not need the extra " which you are using
<cfcase value="6"><cfset CATEGORY6 = 1></cfcase>
<cfcase value="7"><cfset CATEGORY7= 2></cfcase>
Regards,
---Pinal
You already running a loop over CAT variables
Now this CAT variable is in cfcase.
So when the cfcase statement is execuated you already know what is the cat because you are writing the speical condition for that.
For example in the line
<cfcase value="6"><cfset CATEGORY#cat# = "1"></cfcase>*****HERE'S WHERE THE ERROR OCCURS******
<cfcase value="7"><cfset CATEGORY#cat# = "3"></cfcase>
you already know that your cat is 6 and 7 . in that case... why not write direct cfset as follows because programmatically this way is more clear and organized.
<cfcase value="6"><cfset CATEGORY6 = "1"></cfcase>
<cfcase value="7"><cfset CATEGORY7= "3"></cfcase>
Also if you using integer may be you do not need the extra " which you are using
<cfcase value="6"><cfset CATEGORY6 = 1></cfcase>
<cfcase value="7"><cfset CATEGORY7= 2></cfcase>
Regards,
---Pinal
<cfset setvariable('category#cat#
But the above example will work just as fine.