<cfif StructKeyExists(pStruct,"KEY") is true>
<trace text="KEY found">
<cfset lvKEY = StructFind(pStruct,"KEY")>
the StructFind function generates the error:-
message=Cannot find key KEY in struct.
detail=The specified key, KEY, does not exist in the structure.
Yet the StructKeyExists function says it exists.
This code is in a function and pStruct is:-
<cfargument name="pStruct" type="Struct" required="true">
I'm using CF7.02.
Why is this happening?
Chris
--
Chris Velevitch
Manager - Sydney Flash Platform Developers Group
m: 0415 469 095
www.flashdev.org.au
structKeyExists takes the name of the structure
try
<cfif StructKeyExists("pStruct","KEY") is true>
StructFind: Determines the value associated with a key in a structure.
Returns the value associated with a key in a structure; if structure
does not exist, throws an exception.
Syntax: StructFind(structure, key)
See: http://livedocs.macromedia.com/coldfusion/7/htmldocs/00000638.htm
StructKeyExists: Determines whether a specific key is present in a
structure. Returns true, if key is in structure; if structure does not
exist, ColdFusion throws an exception.
Syntax: StructKeyExists(structure, "key")
See: http://livedocs.macromedia.com/coldfusion/7/htmldocs/00000645.htm
This generates an error.
It's not the StructKeyExists function that fails, it's the StructFind
and to make matters worse, it's intermittent.
<cfset lvKEY = StructFind(pStruct, KEY)>
anyhoo:
what do you get with
<cfif StructKeyExists(pStruct,"KEY") is true>
<cfoutput>#StructKeyList(pStruct)#</cfoutput>
is "KEY" in there then?
is the CFC you that is calling this is a shared scope? (EG:
Application scope)
The reason I ask is because in the past I have seen things like this
because of un-"var"ed variables.
If two threads call the CFC at the same time, it can have odd results
and/or errors.
Just a thought...
I log a StructKeyList before the cfif and it's there.
It's not a cfc, it's a cfm with a function and it's called via Flash Remoting.
I once had a pretty weird issue with dynamically created struct keys
that had invisible
whitespace-type special chars at the beginning/end of the string and had
similar error
messages. Have you tried to make sure that "KEY" (I assume that would be
a variable)
is trimmed and use the result of trim() as the key?
Cheers
Kai
Good point. I my case it's a constant and it's intermittent. Sometimes
it works, sometimes it doesn't work. I all cases, I log the output of
StructKeyList and it's always there.
Is it possible that the Struct has a null value against the key?
If there is a null value against the key, then it's quite likely that
the StructKeyExists will return true, but the StruftFind call will
fail as described.
Mark
On 9/17/07, Chris Velevitch <chris.v...@gmail.com> wrote:
>
> On 9/17/07, Kai Koenig <k...@bloginblack.de> wrote:
> > I once had a pretty weird issue with dynamically created struct keys
> > that had invisible
> > whitespace-type special chars at the beginning/end of the string and had
> > similar error
> > messages. Have you tried to make sure that "KEY" (I assume that would be
> > a variable)
> > is trimmed and use the result of trim() as the key?
structFind(struct, key)
instead of just
struct[key]
?
I'm not sure if this would have any bearing on your issue, but it
might be interesting to see the difference in behaviour.
--
Adam
If you try and output "struct[key]" and the key doesn't exists that it will
error
I think maybe you have confused the meaning of your message :)
structFind determines if there is a value for a key in a struct. And
it does NOT have much use IMHO.
--
===========================================================================
Raymond Camden, Camden Media
Email : r...@camdenfamily.com
Blog : www.coldfusionjedi.com
AOL IM : cfjedimaster
Keep up to date with the community: http://www.coldfusionbloggers.org
Sure. As with structFind().
Hence my question.
> I think maybe you have confused the meaning of your message :)
Maybe. Or maybe... not.
--
Adam
So Inside a CFC you would find me writing:
<cfset thisValue = StructFind(getStruct(), key) />
As ColdFusion doesn't support syntax like:
<cfset thisValue = getStruct()[key] />
Mark
But the truth of the matter is that it is depending on what you intend to do
with the results.
StructFind() : Will throw an exception error if the key doesn't exist.
StructKeyExists() : will return Yes / no if the key exists or not.
Struct["Key"] : Will also throw an exception if the key doesn't exist.
Now you could always wrap a try/catch around most tags for exception
handling, but this is an expensive way of doing things.
Andrew Scott
Senior Coldfusion Developer
Aegeon Pty. Ltd.
www.aegeon.com.au
Phone: +613 8676 4223
Mobile: 0404 998 273
-----Original Message-----
From: cfau...@googlegroups.com [mailto:cfau...@googlegroups.com] On Behalf
Of Mark Mandel
Sent: Wednesday, 19 September 2007 9:08 AM
To: cfau...@googlegroups.com
Subject: [cfaussie] Re: Cannot find key in struct.
<cfset myStruct = StructNew()/>
<cfset myStruct.key1 = "Hello"/>
<cfset myStruct.key2 = JavaCast("null", "")/>
<cfset myStruct["key3"] = JavaCast("null", "")/>
<cfset myStruct.put( "key4", JavaCast("null", ""))/>
<cfoutput>
Key1 Exists: #StructKeyExists(myStruct, "key1")#<br/>
Key2 Exists: #StructKeyExists(myStruct, "key2")#<br/>
Key3 Exists: #StructKeyExists(myStruct, "key3")#<br/>
Key4 Exists: #StructKeyExists(myStruct, "key4")#<br/>
Keys: #StructKeyList(myStruct)#<br/>
</cfoutput>
Output:
Key1 Exists: YES
Key2 Exists: NO
Key3 Exists: NO
Key4 Exists: NO
Keys: KEY1,KEY2,key3,key4
So, the StructKeyExists() does not return "true" if the value is null
(at least not with CF8)
BUT, as you can see, the StructKeyList does indeed list the all the
Keys regardless of their corresponding value. So maybe CF less then 8
behaves differently.
/Max
The reason being is that Java, as I see it when defining something as NULL
is telling you that it actually doesn't exist. Because come GC time it will
be removed from the system quickly.
However Coldfusion is obviously still setting the variable in the struct
class object, and is saying the correct thing when checking if it exists,
but the list of keys is wrong because it is held in the scope structure as
being defined.
Very interesting find if you ask me...
Andrew Scott
Senior Coldfusion Developer
Aegeon Pty. Ltd.
www.aegeon.com.au
Phone: +613 8676 4223
Mobile: 0404 998 273
-----Original Message-----
From: cfau...@googlegroups.com [mailto:cfau...@googlegroups.com] On Behalf
Of Maximilian Nyman
Sent: Wednesday, 19 September 2007 10:59 AM
To: cfau...@googlegroups.com
Subject: [cfaussie] Re: Cannot find key in struct.
Andrew, I beg to differ
for me NULL tells me that it has uninitialized memory allocated to it.
It has memory but no value. Coming from a Microsoft world, this is
especially important if you're dealing with late verses early binding
when creating and initializing objects.
However if you try and use something that doesn't exist - that has no
memory allocation - then it *should* throw an error.
ColdFusion not having explicit NULL's is just muddying the issue. if
it doesn't exist, it's not there.
That's my point, if something remains null come GC it's no longer available.
In one instance, the StructKeyExists says no it doesn't exist this part is
true.
The other case StructKeyList, says that it does exist this part I believe is
wrong. Because technically, no memory allocated means it doesn't exist as
you stated.
Andrew Scott
Senior Coldfusion Developer
Aegeon Pty. Ltd.
www.aegeon.com.au
Phone: +613 8676 4223
Mobile: 0404 998 273
-----Original Message-----
From: cfau...@googlegroups.com [mailto:cfau...@googlegroups.com] On Behalf
Of Barry Beattie
Sent: Wednesday, 19 September 2007 12:30 PM
To: cfau...@googlegroups.com
Subject: [cfaussie] Re: Cannot find key in struct.
Go for it, I don't mind at all :oD
Mark
The struct is passed in from a Flash client using Flash Remoting.
> Is it possible that the Struct has a null value against the key?
Yes, that could be possible in Flash to create a key assign it a null value.
I'll look into that. Thanks.
It's only relevant if one doesn't know the key name already, as one
CAN do this, quite happily:
getStruct().key
So this means it's only useful in a situation in which we're using a
runtime value for the key name, AND we don't want to actually use the
struct for anything other than getting that one key, because if one
wanted another key, one would need to call getStruct() again, and
that's a bit lazy.
And at the base level, it doesn't quite sit right with me that one
would be using a getter for a "non-predefined". It's as if one
doesn't know what member variables one's object has. Which... I
dunno, doesn't seem "right".
What's a real world use case for this technique, Mark?
Sure, it demonstrates a usage of structFind(), which was what you're
pointing out, but I'm not sure how often it's a technique that would
be something to recommend.
--
Adam
Which is exactly where I use it. If I knew the key already, I would
have written an example like:
StructFind(getStruct(), "key");
> getStruct().key
>
> So this means it's only useful in a situation in which we're using a
> runtime value for the key name, AND we don't want to actually use the
> struct for anything other than getting that one key, because if one
> wanted another key, one would need to call getStruct() again, and
> that's a bit lazy.
>
> And at the base level, it doesn't quite sit right with me that one
> would be using a getter for a "non-predefined". It's as if one
> doesn't know what member variables one's object has. Which... I
> dunno, doesn't seem "right".
Wow... that's a lot of supposition on surrounding code on a one line
piece of code ;o)
>
> What's a real world use case for this technique, Mark?
Here is a whole bunch:
http://www.koders.com/default.aspx?s=StructFind&btn=&la=ColdFusion&li=*
This is not to say that I couldn't have set the structure to a
variable, and then done it that way, but it's just another option when
I want to simply get something out of a struct, that is encapsulated
in a getter and a setter.
>
> Sure, it demonstrates a usage of structFind(), which was what you're
> pointing out, but I'm not sure how often it's a technique that would
> be something to recommend.
I still don't see how it could be 'bad' ? It's the same thing as
going struct[key] but, with a method syntax? Is somehow struct[key]
bad?
I'm really actually confused by this response.
Mark