Using variables in a Switch case?
CF 5 doesn't like this: cfset nID = 3 cfset nTest1 = 2 cfset nTest2 = 3 cfswitch expression=#nID# cfcase value=#nTest1# brTwo /cfcase cfcase value=#nTest2# brthree /cfcase /cfswitch It throws an error saying the cfcase must have a constant value. So, if I do this instead: cfcase value=2 brTwo /cfcase cfcase value=3 brthree /cfcase all works as expected. The downside is that we have a large number of items where their IDs will be changing on a regular basis (update, and replace in a different system assigns a new ID - our code is keyed on that ID). We are assigning these IDs into a structure in application.cfm to minimize the number of changes we need to make when the IDs change. But, this switch block doesn't like that plan. Is there any way to do this with the variables? Thanks in advance. Shawn ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribeforumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq This list and all House of Fusion resources hosted by CFHosting.com. The place for dependable ColdFusion Hosting. Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Using variables in a Switch case?
Uhm.. If..elseif..elseif..elseif..etc..else..endif Cfswitch is equivalent to cfscript's switch which is equivalent to most C-like languages' switch statements which REQUIRES literals in its case labels. :P This is not specific to CFML v5. CFMX has the same problem. --- James Ang Sr. Developer/Product Engineer MedSeek, Inc. [EMAIL PROTECTED] -Original Message- From: Shawn Grover [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 26, 2003 2:46 PM To: CF-Talk Subject: Using variables in a Switch case? CF 5 doesn't like this: cfset nID = 3 cfset nTest1 = 2 cfset nTest2 = 3 cfswitch expression=#nID# cfcase value=#nTest1# brTwo /cfcase cfcase value=#nTest2# brthree /cfcase /cfswitch It throws an error saying the cfcase must have a constant value. So, if I do this instead: cfcase value=2 brTwo /cfcase cfcase value=3 brthree /cfcase all works as expected. The downside is that we have a large number of items where their IDs will be changing on a regular basis (update, and replace in a different system assigns a new ID - our code is keyed on that ID). We are assigning these IDs into a structure in application.cfm to minimize the number of changes we need to make when the IDs change. But, this switch block doesn't like that plan. Is there any way to do this with the variables? Thanks in advance. Shawn ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribeforumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Signup for the Fusion Authority news alert and keep up with the latest news in ColdFusion and related topics. http://www.fusionauthority.com/signup.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Using variables in a Switch case?
You could do this: -- main_template.cfm -- cfset nID = 3 / cfinclude template=case_#nID#.cfm / -- case_2.cfm -- br /Two -- case_3.cfm -- br /three Not quite as easy to edit a several cases at once, but if the cases are reasonably separate, that might provide exactly the behaviour you want. barneyb --- Barney Boisvert, Senior Development Engineer AudienceCentral (formerly PIER System, Inc.) [EMAIL PROTECTED] voice : 360.765.8080 x12 fax : 360.647.5351 www.audiencecentral.com -Original Message- From: Shawn Grover [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 26, 2003 2:46 PM To: CF-Talk Subject: Using variables in a Switch case? CF 5 doesn't like this: cfset nID = 3 cfset nTest1 = 2 cfset nTest2 = 3 cfswitch expression=#nID# cfcase value=#nTest1# brTwo /cfcase cfcase value=#nTest2# brthree /cfcase /cfswitch It throws an error saying the cfcase must have a constant value. So, if I do this instead: cfcase value=2 brTwo /cfcase cfcase value=3 brthree /cfcase all works as expected. The downside is that we have a large number of items where their IDs will be changing on a regular basis (update, and replace in a different system assigns a new ID - our code is keyed on that ID). We are assigning these IDs into a structure in application.cfm to minimize the number of changes we need to make when the IDs change. But, this switch block doesn't like that plan. Is there any way to do this with the variables? Thanks in advance. Shawn ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribeforumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Your ad could be here. Monies from ads go to support these lists and provide more resources for the community. http://www.fusionauthority.com/ads.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Using variables in a Switch case?
Thanks for the replys. I was hoping to avoid if... elseif... elseif... else type code. And I don't think the dynamic includes are the best answer in our case (although it is a creative solution that might work for us elsewhere...). I think we're stuck with changing our IDs in the application.cfm, AND the page with the switch block. Thanks anyways. Shawn -Original Message- From: Barney Boisvert [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 26, 2003 4:07 PM To: CF-Talk Subject: RE: Using variables in a Switch case? You could do this: -- main_template.cfm -- cfset nID = 3 / cfinclude template=case_#nID#.cfm / -- case_2.cfm -- br /Two -- case_3.cfm -- br /three Not quite as easy to edit a several cases at once, but if the cases are reasonably separate, that might provide exactly the behaviour you want. barneyb --- Barney Boisvert, Senior Development Engineer AudienceCentral (formerly PIER System, Inc.) [EMAIL PROTECTED] voice : 360.765.8080 x12 fax : 360.647.5351 www.audiencecentral.com -Original Message- From: Shawn Grover [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 26, 2003 2:46 PM To: CF-Talk Subject: Using variables in a Switch case? CF 5 doesn't like this: cfset nID = 3 cfset nTest1 = 2 cfset nTest2 = 3 cfswitch expression=#nID# cfcase value=#nTest1# brTwo /cfcase cfcase value=#nTest2# brthree /cfcase /cfswitch It throws an error saying the cfcase must have a constant value. So, if I do this instead: cfcase value=2 brTwo /cfcase cfcase value=3 brthree /cfcase all works as expected. The downside is that we have a large number of items where their IDs will be changing on a regular basis (update, and replace in a different system assigns a new ID - our code is keyed on that ID). We are assigning these IDs into a structure in application.cfm to minimize the number of changes we need to make when the IDs change. But, this switch block doesn't like that plan. Is there any way to do this with the variables? Thanks in advance. Shawn ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribeforumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq This list and all House of Fusion resources hosted by CFHosting.com. The place for dependable ColdFusion Hosting. Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Using variables in a Switch case?
James wrote: Uhm.. If..elseif..elseif..elseif..etc..else..endif and CFMX has the same problem. And rest assured it is a problem that is not afflicted on every language. I posted a feature request for this functionality and some others to Macromedia and it didn't get anywhere. My impression was the speed of the tag would have been degraded in this environment if such a feature had been added. Shame because it is incredibly powerful. Lets say, for example, you have a chunk of code that has to branch in one of 50 directions, and those directions are variable depending on stuff that happened further up the pipe. Compare the exercution time of hobbling thru 50 elseifs versus one case statement where the only code that gets executed is the case you need at the moment. Or wouldn't it be nice to be able to code this: select case zipcode% case 93701:territory%=1 case is 93702 to 93750:territory%=2 case is 93751, 93752, 93754, 93755 to 94900:territory%=3 case 94901:territory%=4 case else:territory%=5 end case Can you write this some other way? Yes. As elegantly? I had to translate an insurance company's entire rating system into CF and I sorely missed the ability to use variables and ranges in case statements. So would you if you had the opportunity to use them. I'd like to see something like this in CF still. Never mind the speed. Look at the functionality. --- Matt Robertson, [EMAIL PROTECTED] MSB Designs, Inc. http://mysecretbase.com --- ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribeforumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Using variables in a Switch case?
CF gets enough criticism about being slow. Adding functionality doesn't mean that developers will understand the consequenses of using it. They'll just complain that it's slow, even if the docs explicitly state that using such code is not recommended in most circuimstances because of performance issues. I know I've never read the docs on the CFSWITCH..CFCASE tags, and I know I never will, I'll just end up trying a dynamic case by accident one time, not get a syntax error, and then wonder why my app is running slow all of a sudden. However, adding an attribute to the CFSWITCH tag that allows for dynamic cases might be an option. Just make sure it's documented as slow, because it manually converts the switch into a set of if..elseif..else statements during compilation to save typing. Then you wouldn't have as much problem with unexpected slowdowns. --- Barney Boisvert, Senior Development Engineer AudienceCentral (formerly PIER System, Inc.) [EMAIL PROTECTED] voice : 360.756.8080 x12 fax : 360.647.5351 www.audiencecentral.com -Original Message- From: Matt Robertson [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 26, 2003 3:26 PM To: CF-Talk Subject: RE: Using variables in a Switch case? James wrote: Uhm.. If..elseif..elseif..elseif..etc..else..endif and CFMX has the same problem. And rest assured it is a problem that is not afflicted on every language. I posted a feature request for this functionality and some others to Macromedia and it didn't get anywhere. My impression was the speed of the tag would have been degraded in this environment if such a feature had been added. Shame because it is incredibly powerful. Lets say, for example, you have a chunk of code that has to branch in one of 50 directions, and those directions are variable depending on stuff that happened further up the pipe. Compare the exercution time of hobbling thru 50 elseifs versus one case statement where the only code that gets executed is the case you need at the moment. Or wouldn't it be nice to be able to code this: select case zipcode% case 93701:territory%=1 case is 93702 to 93750:territory%=2 case is 93751, 93752, 93754, 93755 to 94900:territory%=3 case 94901:territory%=4 case else:territory%=5 end case Can you write this some other way? Yes. As elegantly? I had to translate an insurance company's entire rating system into CF and I sorely missed the ability to use variables and ranges in case statements. So would you if you had the opportunity to use them. I'd like to see something like this in CF still. Never mind the speed. Look at the functionality. --- Matt Robertson, [EMAIL PROTECTED] MSB Designs, Inc. http://mysecretbase.com --- ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribeforumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Your ad could be here. Monies from ads go to support these lists and provide more resources for the community. http://www.fusionauthority.com/ads.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Using variables in a Switch case?
I think the execution time for the if/elseif/else code would be the same as dynamic cfcases (if the feature is introduced and used). :P The only reason cfswitch is faster than if/elseif/else is that at compile time, the values for the cfcases are known. The comparison operations would be easy (no memory lookups, and you could use labels and gotos in the pcode/machine code). If you have written assembly or a compiler, you would know what I mean. :P Nevertheless, I am not disputing that the syntactic elegance (I just love syntactic sugar!) of dynamic cfcase. :) Maybe MACR CAN introduce this feature by ONLY compiling to the real switch/case of the C/Java equivalents when ALL CFCASES are literals else convert the statement to a bunch of if/elseif/else. That may not be too hard to do. I am not sure. :) The documentation should warn the user of the performance impact of using dynamic cfcase though. ;) Care to comment, Mr. Sean Corfield? :P I know some hardcore programmers are probably groaning now. ;) --- James Ang Sr. Developer/Product Engineer MedSeek, Inc. [EMAIL PROTECTED] -Original Message- From: Matt Robertson [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 26, 2003 3:26 PM To: CF-Talk Subject: RE: Using variables in a Switch case? James wrote: Uhm.. If..elseif..elseif..elseif..etc..else..endif and CFMX has the same problem. And rest assured it is a problem that is not afflicted on every language. I posted a feature request for this functionality and some others to Macromedia and it didn't get anywhere. My impression was the speed of the tag would have been degraded in this environment if such a feature had been added. Shame because it is incredibly powerful. Lets say, for example, you have a chunk of code that has to branch in one of 50 directions, and those directions are variable depending on stuff that happened further up the pipe. Compare the exercution time of hobbling thru 50 elseifs versus one case statement where the only code that gets executed is the case you need at the moment. Or wouldn't it be nice to be able to code this: select case zipcode% case 93701:territory%=1 case is 93702 to 93750:territory%=2 case is 93751, 93752, 93754, 93755 to 94900:territory%=3 case 94901:territory%=4 case else:territory%=5 end case Can you write this some other way? Yes. As elegantly? I had to translate an insurance company's entire rating system into CF and I sorely missed the ability to use variables and ranges in case statements. So would you if you had the opportunity to use them. I'd like to see something like this in CF still. Never mind the speed. Look at the functionality. --- Matt Robertson, [EMAIL PROTECTED] MSB Designs, Inc. http://mysecretbase.com --- ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribeforumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Signup for the Fusion Authority news alert and keep up with the latest news in ColdFusion and related topics. http://www.fusionauthority.com/signup.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Using variables in a Switch case?
I knew when I posted that I'd be in for it :) Like I said I converted a gigantic app into CF that had loads of this sort of stuff in it. Very complex U.S. zip code territory stuff, among other things, that relied on this sort of functionality and had to all be rewritten. The original runs amazingly fast for its size, but then again its not a web app. --- Matt Robertson, [EMAIL PROTECTED] MSB Designs, Inc. http://mysecretbase.com --- -- Original Message -- From: James Ang [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] date: Wed, 26 Mar 2003 15:42:09 -0800 I think the execution time for the if/elseif/else code would be the same as dynamic cfcases (if the feature is introduced and used). :P The only reason cfswitch is faster than if/elseif/else is that at compile time, the values for the cfcases are known. The comparison operations would be easy (no memory lookups, and you could use labels and gotos in the pcode/machine code). If you have written assembly or a compiler, you would know what I mean. :P Nevertheless, I am not disputing that the syntactic elegance (I just love syntactic sugar!) of dynamic cfcase. :) Maybe MACR CAN introduce this feature by ONLY compiling to the real switch/case of the C/Java equivalents when ALL CFCASES are literals else convert the statement to a bunch of if/elseif/else. That may not be too hard to do. I am not sure. :) The documentation should warn the user of the performance impact of using dynamic cfcase though. ;) Care to comment, Mr. Sean Corfield? :P I know some hardcore programmers are probably groaning now. ;) --- James Ang Sr. Developer/Product Engineer MedSeek, Inc. [EMAIL PROTECTED] -Original Message- From: Matt Robertson [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 26, 2003 3:26 PM To: CF-Talk Subject: RE: Using variables in a Switch case? James wrote: Uhm.. If..elseif..elseif..elseif..etc..else..endif and CFMX has the same problem. And rest assured it is a problem that is not afflicted on every language. I posted a feature request for this functionality and some others to Macromedia and it didn't get anywhere. My impression was the speed of the tag would have been degraded in this environment if such a feature had been added. Shame because it is incredibly powerful. Lets say, for example, you have a chunk of code that has to branch in one of 50 directions, and those directions are variable depending on stuff that happened further up the pipe. Compare the exercution time of hobbling thru 50 elseifs versus one case statement where the only code that gets executed is the case you need at the moment. Or wouldn't it be nice to be able to code this: select case zipcode% case 93701:territory%=1 case is 93702 to 93750:territory%=2 case is 93751, 93752, 93754, 93755 to 94900:territory%=3 case 94901:territory%=4 case else:territory%=5 end case Can you write this some other way? Yes. As elegantly? I had to translate an insurance company's entire rating system into CF and I sorely missed the ability to use variables and ranges in case statements. So would you if you had the opportunity to use them. I'd like to see something like this in CF still. Never mind the speed. Look at the functionality. --- Matt Robertson, [EMAIL PROTECTED] MSB Designs, Inc. http://mysecretbase.com --- ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribeforumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Get the mailserver that powers this list at http://www.coolfusion.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Re: Using variables in a Switch case?
If you have a situation with 50 possible branchs, with 50 different pieces of logic (imagine that flowchart! lol), hard-coding the branches conditions would seem at least from my point of view, the wrong way to go about it. If you already have 50, the chances are that the conditions will change as business rules change later on (as in the case of zip codes). Just looking at your code, I'd think sticking all the zip's in a database with their corresponding numbers (1,2,3...looks like only 5 possible branches in the code below?) and simply select out the necessary data would be more elegant, and need less tinkering as business rules change. Not that it wouldn't be nice to have the option to have dynamic case statements, but you asked :) -- jon mailto:[EMAIL PROTECTED] Wednesday, March 26, 2003, 6:25:34 PM, you wrote: MR James wrote: Uhm.. If..elseif..elseif..elseif..etc..else..endif MR and CFMX has the same problem. MR And rest assured it is a problem that is not afflicted on every language. I posted a feature request for this functionality and some others to Macromedia and it didn't get anywhere. My MR impression was the speed of the tag would have been degraded in this environment if such a feature had been added. Shame because it is incredibly powerful. MR Lets say, for example, you have a chunk of code that has to branch in one of 50 directions, and those directions are variable depending on stuff that happened further up the pipe. MR Compare the exercution time of hobbling thru 50 elseifs versus one case statement where the only code that gets executed is the case you need at the moment. MR Or wouldn't it be nice to be able to code this: MR select case zipcode% MR case 93701:territory%=1 MR case is 93702 to 93750:territory%=2 MR case is 93751, 93752, 93754, 93755 to 94900:territory%=3 MR case 94901:territory%=4 MR case else:territory%=5 MR end case MR Can you write this some other way? Yes. As elegantly? I had to translate an insurance company's entire rating system into CF and I sorely missed the ability to use variables and ranges in case MR statements. So would you if you had the opportunity to use them. MR I'd like to see something like this in CF still. Never mind the speed. Look at the functionality. MR --- MR Matt Robertson, [EMAIL PROTECTED] MR MSB Designs, Inc. http://mysecretbase.com MR --- MR ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribeforumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Get the mailserver that powers this list at http://www.coolfusion.com Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
RE: Using variables in a Switch case?
How about this: cfset zipcode = 99 cf_dynamicswitch expression=#zipcode# return=territory cf_dynamiccase rule=value eq 93701 result=1 cf_dynamiccase rule=value gte 93702 and value lte 93750 result=2 cf_dynamiccase rule=listfind(93751,93752,93754, value) or (value gte 93755 and value lte 94900) result=3 cf_dynamiccase rule=value gte 94901 result=4 cf_dynamiccase rule=true result=5 /cf_dynamicswitch cfoutput#territory#/cfoutput Here are the custom tags. It's just a first attempt as an experiment. It relies on evaluate() but it's easy to read and not slow on small examples. Dynamicswitch.cfm: cfif NOT ThisTag.HasEndTag cfabort showerror=DYNAMICSWITCH Error: CF_dynamicswitch must have an end tag. /cfif cfif ThisTag.executionmode eq end cfdump var=#ThisTag# cfset value = attributes.expression cfloop index=i from=1 to=#arraylen(thistag.cases)# cfif evaluate(thistag.cases[i].rule) cfset caller[attributes.return] = thistag.cases[i].result cfbreak /cfif /cfloop /cfif dynamiccase.cfm: cfsilent cfif NOT ListFind(GetBaseTagList(),CF_DYNAMICSWITCH) cfabort showerror=DYNAMICSWITCH Error: CF_dynamiccase must be nested inside a CF_dynamicswitch tag pair. /cfif cfif ThisTag.ExecutionMode eq start cfassociate basetag=CF_dynamicswitch datacollection=cases /cfif /cfsilent Matthew Walker Electric Sheep Web http://www.electricsheep.co.nz/ -Original Message- From: Matt Robertson [mailto:[EMAIL PROTECTED] Sent: Thursday, 27 March 2003 11:53 a.m. To: CF-Talk Subject: RE: Using variables in a Switch case? I knew when I posted that I'd be in for it :) Like I said I converted a gigantic app into CF that had loads of this sort of stuff in it. Very complex U.S. zip code territory stuff, among other things, that relied on this sort of functionality and had to all be rewritten. The original runs amazingly fast for its size, but then again its not a web app. --- Matt Robertson, [EMAIL PROTECTED] MSB Designs, Inc. http://mysecretbase.com --- -- Original Message -- From: James Ang [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] date: Wed, 26 Mar 2003 15:42:09 -0800 I think the execution time for the if/elseif/else code would be the same as dynamic cfcases (if the feature is introduced and used). :P The only reason cfswitch is faster than if/elseif/else is that at compile time, the values for the cfcases are known. The comparison operations would be easy (no memory lookups, and you could use labels and gotos in the pcode/machine code). If you have written assembly or a compiler, you would know what I mean. :P Nevertheless, I am not disputing that the syntactic elegance (I just love syntactic sugar!) of dynamic cfcase. :) Maybe MACR CAN introduce this feature by ONLY compiling to the real switch/case of the C/Java equivalents when ALL CFCASES are literals else convert the statement to a bunch of if/elseif/else. That may not be too hard to do. I am not sure. :) The documentation should warn the user of the performance impact of using dynamic cfcase though. ;) Care to comment, Mr. Sean Corfield? :P I know some hardcore programmers are probably groaning now. ;) --- James Ang Sr. Developer/Product Engineer MedSeek, Inc. [EMAIL PROTECTED] -Original Message- From: Matt Robertson [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 26, 2003 3:26 PM To: CF-Talk Subject: RE: Using variables in a Switch case? James wrote: Uhm.. If..elseif..elseif..elseif..etc..else..endif and CFMX has the same problem. And rest assured it is a problem that is not afflicted on every language. I posted a feature request for this functionality and some others to Macromedia and it didn't get anywhere. My impression was the speed of the tag would have been degraded in this environment if such a feature had been added. Shame because it is incredibly powerful. Lets say, for example, you have a chunk of code that has to branch in one of 50 directions, and those directions are variable depending on stuff that happened further up the pipe. Compare the exercution time of hobbling thru 50 elseifs versus one case statement where the only code that gets executed is the case you need at the moment. Or wouldn't it be nice to be able to code this: select case zipcode% case 93701:territory%=1 case is 93702 to 93750:territory%=2 case is 93751, 93752, 93754, 93755 to 94900:territory%=3 case 94901:territory%=4 case else:territory%=5 end case Can you write this some other way? Yes. As elegantly? I had to translate an insurance company's entire rating system into CF and I sorely
RE: Using variables in a Switch case?
And of course removing the cfdump wouldn't hurt ;-) -Original Message- From: Matthew Walker [mailto:[EMAIL PROTECTED] Sent: Thursday, 27 March 2003 12:01 p.m. To: CF-Talk Subject: RE: Using variables in a Switch case? How about this: cfset zipcode = 99 cf_dynamicswitch expression=#zipcode# return=territory cf_dynamiccase rule=value eq 93701 result=1 cf_dynamiccase rule=value gte 93702 and value lte 93750 result=2 cf_dynamiccase rule=listfind(93751,93752,93754, value) or (value gte 93755 and value lte 94900) result=3 cf_dynamiccase rule=value gte 94901 result=4 cf_dynamiccase rule=true result=5 /cf_dynamicswitch cfoutput#territory#/cfoutput Here are the custom tags. It's just a first attempt as an experiment. It relies on evaluate() but it's easy to read and not slow on small examples. Dynamicswitch.cfm: cfif NOT ThisTag.HasEndTag cfabort showerror=DYNAMICSWITCH Error: CF_dynamicswitch must have an end tag. /cfif cfif ThisTag.executionmode eq end cfdump var=#ThisTag# cfset value = attributes.expression cfloop index=i from=1 to=#arraylen(thistag.cases)# cfif evaluate(thistag.cases[i].rule) cfset caller[attributes.return] = thistag.cases[i].result cfbreak /cfif /cfloop /cfif dynamiccase.cfm: cfsilent cfif NOT ListFind(GetBaseTagList(),CF_DYNAMICSWITCH) cfabort showerror=DYNAMICSWITCH Error: CF_dynamiccase must be nested inside a CF_dynamicswitch tag pair. /cfif cfif ThisTag.ExecutionMode eq start cfassociate basetag=CF_dynamicswitch datacollection=cases /cfif /cfsilent Matthew Walker Electric Sheep Web http://www.electricsheep.co.nz/ -Original Message- From: Matt Robertson [mailto:[EMAIL PROTECTED] Sent: Thursday, 27 March 2003 11:53 a.m. To: CF-Talk Subject: RE: Using variables in a Switch case? I knew when I posted that I'd be in for it :) Like I said I converted a gigantic app into CF that had loads of this sort of stuff in it. Very complex U.S. zip code territory stuff, among other things, that relied on this sort of functionality and had to all be rewritten. The original runs amazingly fast for its size, but then again its not a web app. --- Matt Robertson, [EMAIL PROTECTED] MSB Designs, Inc. http://mysecretbase.com --- -- Original Message -- From: James Ang [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] date: Wed, 26 Mar 2003 15:42:09 -0800 I think the execution time for the if/elseif/else code would be the same as dynamic cfcases (if the feature is introduced and used). :P The only reason cfswitch is faster than if/elseif/else is that at compile time, the values for the cfcases are known. The comparison operations would be easy (no memory lookups, and you could use labels and gotos in the pcode/machine code). If you have written assembly or a compiler, you would know what I mean. :P Nevertheless, I am not disputing that the syntactic elegance (I just love syntactic sugar!) of dynamic cfcase. :) Maybe MACR CAN introduce this feature by ONLY compiling to the real switch/case of the C/Java equivalents when ALL CFCASES are literals else convert the statement to a bunch of if/elseif/else. That may not be too hard to do. I am not sure. :) The documentation should warn the user of the performance impact of using dynamic cfcase though. ;) Care to comment, Mr. Sean Corfield? :P I know some hardcore programmers are probably groaning now. ;) --- James Ang Sr. Developer/Product Engineer MedSeek, Inc. [EMAIL PROTECTED] -Original Message- From: Matt Robertson [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 26, 2003 3:26 PM To: CF-Talk Subject: RE: Using variables in a Switch case? James wrote: Uhm.. If..elseif..elseif..elseif..etc..else..endif and CFMX has the same problem. And rest assured it is a problem that is not afflicted on every language. I posted a feature request for this functionality and some others to Macromedia and it didn't get anywhere. My impression was the speed of the tag would have been degraded in this environment if such a feature had been added. Shame because it is incredibly powerful. Lets say, for example, you have a chunk of code that has to branch in one of 50 directions, and those directions are variable depending on stuff that happened further up the pipe. Compare the exercution time of hobbling thru 50 elseifs versus one case statement where the only code that gets executed is the case you need at the moment. Or wouldn't it be nice to be able to code this: select case zipcode% case 93701:territory%=1 case is 93702 to 93750:territory%=2 case is 93751, 93752, 93754
Re: Using variables in a Switch case?
Unfortunately the 5 territory thing was just a simplified example meant to demo the idea. The real thing is quite a bit more complex. And the 50 thing was 50 U.S. states. That one can be safely hardcoded in :D. At one point in an effort to ease maintenance concerns we tried the db route, but on a different aspect of the in-house system. We found that running against the db rather than just in code put on a much higher load. Since this was client/server, if we did the procedure in code it ran on the individual client's memory and processor. If we plugged in a centralized db then we of course introduced a bottleneck into the system. --- Matt Robertson, [EMAIL PROTECTED] MSB Designs, Inc. http://mysecretbase.com --- -- Original Message -- From: jon hall [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] date: Wed, 26 Mar 2003 18:54:05 -0500 If you have a situation with 50 possible branchs, with 50 different pieces of logic (imagine that flowchart! lol), hard-coding the branches conditions would seem at least from my point of view, the wrong way to go about it. If you already have 50, the chances are that the conditions will change as business rules change later on (as in the case of zip codes). Just looking at your code, I'd think sticking all the zip's in a database with their corresponding numbers (1,2,3...looks like only 5 possible branches in the code below?) and simply select out the necessary data would be more elegant, and need less tinkering as business rules change. Not that it wouldn't be nice to have the option to have dynamic case statements, but you asked :) -- jon mailto:[EMAIL PROTECTED] Wednesday, March 26, 2003, 6:25:34 PM, you wrote: MR James wrote: Uhm.. If..elseif..elseif..elseif..etc..else..endif MR and CFMX has the same problem. MR And rest assured it is a problem that is not afflicted on every language. I posted a feature request for this functionality and some others to Macromedia and it didn't get anywhere. My MR impression was the speed of the tag would have been degraded in this environment if such a feature had been added. Shame because it is incredibly powerful. MR Lets say, for example, you have a chunk of code that has to branch in one of 50 directions, and those directions are variable depending on stuff that happened further up the pipe. MR Compare the exercution time of hobbling thru 50 elseifs versus one case statement where the only code that gets executed is the case you need at the moment. MR Or wouldn't it be nice to be able to code this: MR select case zipcode% MR case 93701:territory%=1 MR case is 93702 to 93750:territory%=2 MR case is 93751, 93752, 93754, 93755 to 94900:territory%=3 MR case 94901:territory%=4 MR case else:territory%=5 MR end case MR Can you write this some other way? Yes. As elegantly? I had to translate an insurance company's entire rating system into CF and I sorely missed the ability to use variables and ranges in case MR statements. So would you if you had the opportunity to use them. MR I'd like to see something like this in CF still. Never mind the speed. Look at the functionality. MR --- MR Matt Robertson, [EMAIL PROTECTED] MR MSB Designs, Inc. http://mysecretbase.com MR --- MR ~| Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4 Subscription: http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribeforumid=4 FAQ: http://www.thenetprofits.co.uk/coldfusion/faq Structure your ColdFusion code with Fusebox. Get the official book at http://www.fusionauthority.com/bkinfo.cfm Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4