views:

84

answers:

6

How would I create this statement in CF?

<cfif (not isdefined("URL.room") or #URL.room# EQ "")
      and (not isdefined("URL.system" or #URL.system# EQ "")
      and (not isdefined("URL.date") or #URL.date# EQ "")>

Obviously the parentheses don't work, but illustrate what I am trying to accomplish. What is the syntax for this?

EDIT: Ok, I understand how to use EQ and all that. I posted this in a bit of a hurry. My question is about the parentheses. Is it syntactically correct to use them this way?

+2  A: 

replace the = with eq

Don
+1  A: 

In CFML the comparison operators use characters rather than symbols:

==  EQ   
!=  NEQ  
>   GT    
>=  GTE   
<   LT    
<=  LTE   

Similarly with boolean operators:

!   NOT
&&  AND
||  OR

You can still use the traditional symbols in CFScript mode.

Also worth mentioning that Railo, an alternative CFML engine to Adobe ColdFusion, allows you to use the symbols in tag-based code, if there is no ambiguity with closing tag (e.g. the condition is wrapped in parentheses).

Peter Boughton
A: 

You need to think your logic through a bit.

You can't check to see if room is an empty string if it is undefined.

Probably what you really need is : If (structkeyexist(URL,"room") and (Len(URL.room) eq 0 or URL.room eq 'blah')) Do something Else Do something else

I'm afraid stackoverflow cuts off your example condition on my phone, but hopefully this illustrates what you need to do.

Stephen Moretti
He's not checking the empty string if it's undefined - the empty string check is only evaluated if the <not isdefined> check returns false - i.e. it *is* defined. It's not easy to follow, which is why I've added an 'inverted' version in my answer.
Peter Boughton
Your code and my code is identical, except that I use structKeyExists() rather than isDefined(). The only problem with my code is that I assumed it was more checks on "room" rather than multiple variables, which I can see now that I can scroll the code box on my pc browser rather than being cut off on my phone browser. :)
Stephen Moretti
+9  A: 

EDIT: Ok, I understand how to use EQ and all that. I posted this in a bit of a hurry. My question is about the parentheses. Is it syntactically correct to use them this way?

Syntactically, yes. The code's syntax is correct and will not throw syntax errors.

However, it's not necessarily the best way to do it. At the very least you should have linebreaks in there, to make it more readable, like so:

<cfif (not isdefined("URL.room") or URL.room EQ "")
    and (not isdefined("URL.system" or URL.system EQ "")
    and (not isdefined("URL.date") or URL.date EQ "")
    >


And I would be more inclined to write it like this:

<cfif NOT
    (  ( isDefined('Url.Room')   AND Len(Url.Room)   )
    OR ( isDefined('Url.System') AND Len(Url.System) )
    OR ( isDefined('Url.Date')   AND Len(Url.Date)   )
    )>

Because that's much more readable, and makes it more obvious that each row is checking the same thing.


That is assuming I was doing this in a single IF statement, anyway.

If you start getting lots of conditions to check, you might want to consider doing something like this instead:

<cfset FieldList = "Room,System,Date" />
<cfset AllFieldsValid = true />
<cfloop index="Field" list="#FieldList#">
    <cfif NOT ( StructKeyExists(Url,Field) AND Len(Url[Field]) )>
        <cfset AllFieldsValid = false />
        <cfbreak/>
    </cfif>
</cfloop>

<cfif AllFieldsValid>
...

Which might look intimidating at first, but is much easier to maintain - you just add a new item to FieldList (and you may already have a variable which serves that purporse).

Anyway, hopefully all this helps - let me know if any questions on it.

Peter Boughton
Thank you for a very thorough answer!
Jimmy
StructKeyExists(Url,'Field') should read StructKeyExists(Url,Field)If I could edit I'd fix this for you ;)
Stephen Moretti
I personally do not like mixing non-boolean function like Len() in a boolean expression. I guess I never remember 0 is true or false..
Henry
Thanks Stephen - could have sworn I was thinking "don't add quotes" as I wrote that line... Oh well.
Peter Boughton
Henry, not sure why the problem with 0=false, it's what all modern languages do? (I vaguely remember something that had 0=true,-1=false but can't recall what it was, and haven't seen anything like that in a long time). Maybe reading it as `hasLen` instead will help. :)
Peter Boughton
+3  A: 

I'd prefer...

<cfparam name="URL.room" default="">
<cfparam name="URL.system" default="">
<cfparam name="URL.date" default="">


<cfif len(URL.room) EQ 0 and len(URL.system) EQ 0 and len(URL.date) EQ 0>
   ...
</cfif>

Or if you're comfortable with missing non-boolean functions with boolean expression

<cfif len(URL.room) and len(URL.system) and len(URL.date)>
   ...
</cfif>
Henry
A: 

@Henry:

 <cfif len(URL.room) EQ 0 and len(URL.system) EQ 0 and len(URL.date) EQ 0>
    ...
 </cfif>

Shorter:

 <CFIF Len(URL.room) AND Len(URL.system) and Len(URL.date)>

Len() is better than EQ ""

da_didi