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 = 999999>

<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 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=subscribe&forumid=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
                                

Reply via email to