contains in cfcase

415 views
Skip to first unread message

manaus

unread,
Oct 11, 2012, 8:17:24 AM10/11/12
to ra...@googlegroups.com
Hello, 
I need to write in cfswitch a statement like this: 

<cfif q.subject contains "word">

This is my try: 

<cfswitch expression="#q.subject#">
  <cfcase value="#find(q.subject, 'word')#">

Needless to say it doesn't work... 

Thanks...

Matt Quackenbush

unread,
Oct 11, 2012, 8:21:24 AM10/11/12
to ra...@googlegroups.com
Pretty sure that you cannot use a dynamic value in the case statement.

JEAguilar

unread,
Oct 29, 2012, 5:06:04 PM10/29/12
to ra...@googlegroups.com
The syntax for Find() is Find(substring, string [, start ]) and it returns "a number; the position of substring in string; or 0, if substring is not in string."

So, let's say that your q.subject is "crossword". The evaluation of Find("word", q.subject) returns 6. So your cfswitch expression:

<cfswitch expression="#q.subject#">
  <cfcase value="#find(q.subject, 'word')#">

is interpreted as:

<cfswitch expression="crossword">
  <cfcase value="6">

So, that case would never occur. 

Seems to me that this is better suited to stick with cfif.
Message has been deleted

Peter Boughton

unread,
Oct 29, 2012, 8:56:35 PM10/29/12
to ra...@googlegroups.com
O_o

I guess it's nearly Halloween; but I only expected to see people making themselves scary - not monstrous code too!
Message has been deleted
Message has been deleted

Rani

unread,
Oct 30, 2012, 7:22:14 AM10/30/12
to ra...@googlegroups.com
@manaus:

It could be done if you are eager to use _cfswitch_, but first you need to evaluate what you have passed in a different way (have tested to Railo 3.3.4.003).

Example code here: http://pastebin.com/FAXm2dAe

Or just:
<cfswitch  expression="#IIF(Find("word",form.myfield) GTE 1, DE("true"), DE("false"))#">

as you would have expected to have in the first place, dinamically; I pretty sure that "the beauty" won''t get angry :-p

@Peter:

I appologies if my original post offended you. i did not know i am not suppose to write examples with more line of codes. I have compacted my previous posts and put the code via pastebin like next time i will. Sorry, once again.


Hope this helps.
best regards

Peter Boughton

unread,
Oct 30, 2012, 10:47:19 AM10/30/12
to ra...@googlegroups.com
Rani wrote:
> I appologies if my original post offended you.
> i did not know i am not suppose to write examples with more line of codes

Huh? Nobody said that. Two dozen lines of code isn't excessive, when relevant.

(The only thing that offends me is ugly and unnecessary characters stuck to the front of my name.)


However, the worst bit of the code you showed is the bit you're still showing...


    <cfswitch  expression="#IIF(Find("word",form.myfield) GTE 1, DE("true"), DE("false"))#">

...and I'm still hoping this is some joke or troll or something, because it really is horribly bad code!

You should never ever use anything like it in a real application.


Even if you'd somehow gone insane, and really needed to use cfswitch instead of cfif, you don't want all that, you just need:

    <cfswitch expression=# ( form.myfield contains 'word' ) # >

But using a switch statement for something that can only ever have two outcomes doesn't make sense.


Also, if you needed to retrieve an arbitrary value based on a boolean, just use the ternary conditional operator:

    <cfset Colour = find("word",form.myfield) ? "green" : "red" />

No need to mess about with IIF or DE any more.

Ed @ ODI Consulting

unread,
Oct 31, 2012, 12:21:07 PM10/31/12
to ra...@googlegroups.com



This is not a job for cfswitch.  Use cfif and cfelseif instead.


On Thursday, October 11, 2012 8:17:24 AM UTC-4, manaus wrote:
Reply all
Reply to author
Forward
0 new messages