House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

Finding if a key exists in a struct based on a variable name

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/07/2010 04:05 PM

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?

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/07/2010 04:15 PM

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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/07/2010 04:37 PM

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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/07/2010 04:48 PM

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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/07/2010 04:55 PM

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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/07/2010 04:59 PM

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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/07/2010 05:04 PM

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>

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Raymond Camden
09/07/2010 05:14 PM

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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/07/2010 05:17 PM

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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Andy Matthews
09/07/2010 05:22 PM

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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/07/2010 06:51 PM

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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
rex
09/07/2010 08:34 PM

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."

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/07/2010 08:49 PM

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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/07/2010 09:30 PM

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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Dominic Watson
09/08/2010 03:24 AM

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

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/08/2010 06:13 AM

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


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

February 08, 2012

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29