|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Finding if a key exists in a struct based on a variable name
If my var "y" = "myCell" and I want to know if the key "myCellHead" existsMichael Grant 09/07/10 04:05 P This should work fine:Raymond Camden 09/07/10 04:15 P I know, but I want it inline of an html tag, so that's why I wanted IIFMichael Grant 09/07/10 04:37 P It works for me:Raymond Camden 09/07/10 04:48 P Here's what I have:Michael Grant 09/07/10 04:55 P Looks pretty similar, but mine works:Raymond Camden 09/07/10 04:59 P Try running this:Michael Grant 09/07/10 05:04 P Bah - I hacked away at it for 10 minutes before remembering why IRaymond Camden 09/07/10 05:14 P HA! So I'm not the only one!Michael Grant 09/07/10 05:17 P CF9 has the ternary operator which is awesome. Works like JavaScript:Andy Matthews 09/07/10 05:22 P Unfortunately I'm on CF8. And I wonder if the ternary operator would be ableMichael Grant 09/07/10 06:51 P A lot of people get DE() wrong.rex 09/07/10 08:34 P Wow. I had no idea you could wrap DE in Evaluate. Did you come figure thisMichael Grant 09/07/10 08:49 P So I went back and read the docs for IIF. I haven't looked at them in yearsMichael Grant 09/07/10 09:30 P On a side note, if you're after an output of 'yes' or 'no', this may be cleaner:Dominic Watson 09/08/10 03:24 A Thanks. I'm actually after the structvalue if it exists and a zero lengthMichael Grant 09/08/10 06:13 A If my var "y" = "myCell" and I want to know if the key "myCellHead" exists in a struct how do I do it? I thought that the following would work in an IIF statement but it doesn't. iif(StructKeyExists(x.classAssign,"#y#head"),DE('yes'),DE('no')) Any ideas? This should work fine: <cfif structKeyExists(somestruct, "#y#head")> You don't need IIF at all. ----- Excess quoted text cut - see Original Post for more ----- I know, but I want it inline of an html tag, so that's why I wanted IIF instead of an if/else block. ----- Excess quoted text cut - see Original Post for more ----- It works for me: <cfset x = {}> <cfset y = "all"> <cfoutput> #iif(StructKeyExists(x,"#y#head"),DE('yes'),DE('no'))# </cfoutput> <p> <cfset x["allhead"] = 1> <cfoutput> #iif(StructKeyExists(x,"#y#head"),DE('yes'),DE('no'))# </cfoutput> I correctly get no/yes. ----- Excess quoted text cut - see Original Post for more ----- Here's what I have: #iif(StructKeyExists(x.classAssign,"#y#head"),DE(x.classAssign["#y#head"]),DE(''))# The error I get is: Element NAMEhead is undefined in a CFML structure referenced as part of an expression. Since I couldn't get it working I switched it to an if/else statement and got the effect I desired, but I don't get why the above code wouldn't work. Even though I've moved on I'd still love to know why it's not working. Thanks. ----- Excess quoted text cut - see Original Post for more ----- Looks pretty similar, but mine works: <cfset x = {}> <cfset y = "all"> <cfoutput> #iif(StructKeyExists(x,"#y#head"),DE('yes'),DE('no'))# </cfoutput> <p> <cfset x["allhead"] = "booger"> <cfoutput> #iif(StructKeyExists(x,"#y#head"),DE(x["#y#head"]),DE('no'))# </cfoutput> ----- Excess quoted text cut - see Original Post for more ----- Try running this: <cfset x = {}> <cfset y = "all"> <cfoutput> #iif(StructKeyExists(x,"#y#head"),DE('yes'),DE('no'))# </cfoutput> <p> <!--- <cfset x["allhead"] = "booger"> ---> <cfoutput> #iif(StructKeyExists(x,"#y#head"),DE(x["#y#head"]),DE('no'))# </cfoutput> Bah - I hacked away at it for 10 minutes before remembering why I hated IIF/DE in the first place. ;) ----- Excess quoted text cut - see Original Post for more ----- HA! So I'm not the only one! So I thought DE meant "Delay Evaluation" as in "Don't evaluate what's in these little brackets this until you've satisfied the IIF condition." I guess not? ----- Excess quoted text cut - see Original Post for more ----- CF9 has the ternary operator which is awesome. Works like JavaScript: <cfset output = (boolean ? "It was True" : "It was False")> andy HA! So I'm not the only one! So I thought DE meant "Delay Evaluation" as in "Don't evaluate what's in these little brackets this until you've satisfied the IIF condition." I guess not? ----- Excess quoted text cut - see Original Post for more ----- Unfortunately I'm on CF8. And I wonder if the ternary operator would be able to handle a dynamic value as the output as with my example. IIF works perfect for exactly the type of example you gave; a simple boolean evaluated to output a simple string. The problem lies in CF apparently evaluating the two outputs before determining if the StruckKey exists. On Tue, Sep 7, 2010 at 5:21 PM, Andy Matthews <lists@commadelimited.com>wrote: ----- Excess quoted text cut - see Original Post for more ----- A lot of people get DE() wrong. IIF does not short-circuit (http://en.wikipedia.org/wiki/Short-circuit_evaluation), meaning that your DE() gets evaluated even if the condition is FALSE. So, this will break: #iif(false, notFalse, false)# since notFalse does not exist. Same here: #iif(true, true, fols)# since fols does not exist. And finally your code: #iif(false, DE(x.classAssign["#y#head"]), DE(''))# breaks since x.classAssign["NAMEhead"] does not exist. DE evaluates a STRING parameter and finds double-quotes. If you pass in a variable, it looks for the value of that variable. Since you are passing x.classAssign["#y#head"], it looks for x.classAssign["NAMEhead"] and breaks. This will work: evaluate(DE("x.classAssign['#y#head']")) - notice the single-quotes surrounding #y#head! This is because we don't want DE to escape this, so we don't want to wrap it around double-quotes! Here is the code (I used "no value" instead of "", but it's still the same code that you use): <cfset x.classAssign = { NameHead = "this head", NoNameHead = "that head" } /> <cfoutput> <cfset y = "Name" /> #iif(StructKeyExists(x.classAssign,"#y#head"), evaluate(DE("x.classAssign['#y#head']")), DE("no value"))#<hr /> <cfset y = "NoExist" /> #iif(StructKeyExists(x.classAssign,"#y#head"), evaluate(DE("x.classAssign['#y#head']")), DE("no value"))#<hr /> <cfset Y = 'Any' /> See how these two differ: <br /> #DE("x.classAssign['#y#head']")#<br /> #DE('x.classAssign["#y#head"]')# </cfoutput> Michael Grant wrote: > HA! So I'm not the only one! > So I thought DE meant "Delay Evaluation" as in "Don't evaluate what's in > these little brackets this until you've satisfied the IIF condition." Wow. I had no idea you could wrap DE in Evaluate. Did you come figure this out through trial and error or have I just never read it? Thanks for the post rex. ----- Excess quoted text cut - see Original Post for more ----- So I went back and read the docs for IIF. I haven't looked at them in years and I'm shocked that I've used it for SOOOOO many years without really knowing exactly how it worked. I would've first read about IIF pre version 5. I can't even find the docs for it. Version 5's description is a little vague. You learn something new every day I guess. That's awesome. Thanks again Rex. ----- Excess quoted text cut - see Original Post for more ----- On a side note, if you're after an output of 'yes' or 'no', this may be cleaner: #YesNoFormat( StructKeyExists(x.classAssign, "#y#head") )# Dominic On 8 September 2010 02:30, Michael Grant <mgrant@modus.bz> wrote: ----- Excess quoted text cut - see Original Post for more ----- Thanks. I'm actually after the structvalue if it exists and a zero length string if it doesn't. It looks like Rex has me sorted though. Thanks Dom. On Wed, Sep 8, 2010 at 3:24 AM, Dominic Watson < watson.dominic@googlemail.com> wrote: ----- Excess quoted text cut - see Original Post for more -----
|
February 08, 2012
|
Latest Fusion Authority Articles
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||