Cannot find key in struct.

807 views
Skip to first unread message

Chris Velevitch

unread,
Sep 16, 2007, 8:11:24 PM9/16/07
to cfau...@googlegroups.com
In the following code:-

<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

AJ Mercer

unread,
Sep 16, 2007, 8:14:14 PM9/16/07
to cfau...@googlegroups.com
mayve try fully scoping the variable
    ARGUMENTS.pStruct

Barry Beattie

unread,
Sep 16, 2007, 8:18:02 PM9/16/07
to cfau...@googlegroups.com
I reckon it's the diff between the name of the structure and the
structure itself for the two diff functions:

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

Chris Velevitch

unread,
Sep 16, 2007, 8:37:06 PM9/16/07
to cfau...@googlegroups.com
On 9/17/07, Barry Beattie <barry....@gmail.com> wrote:
> try
> <cfif StructKeyExists("pStruct","KEY") is true>

This generates an error.

It's not the StructKeyExists function that fails, it's the StructFind
and to make matters worse, it's intermittent.

Barry Beattie

unread,
Sep 16, 2007, 8:55:03 PM9/16/07
to cfau...@googlegroups.com
yeah, I just realised... I actually meant not using the name the
structfind() but that won't help either.

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

David Harris

unread,
Sep 16, 2007, 9:03:12 PM9/16/07
to cfaussie
Hi Chris,

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

Chris Velevitch

unread,
Sep 16, 2007, 9:17:05 PM9/16/07
to cfau...@googlegroups.com
On 9/17/07, Barry Beattie <barry....@gmail.com> wrote:
> what do you get with
>
> <cfif StructKeyExists(pStruct,"KEY") is true>
>
> <cfoutput>#StructKeyList(pStruct)#</cfoutput>
>
> is "KEY" in there then?

I log a StructKeyList before the cfif and it's there.

Chris Velevitch

unread,
Sep 16, 2007, 9:19:05 PM9/16/07
to cfau...@googlegroups.com
On 9/17/07, David Harris <djohn...@gmail.com> wrote:
> is the CFC you that is calling this is a shared scope? (EG:
> Application scope)

It's not a cfc, it's a cfm with a function and it's called via Flash Remoting.

Kai Koenig

unread,
Sep 16, 2007, 9:44:42 PM9/16/07
to cfau...@googlegroups.com
Chris,

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

Chris Velevitch

unread,
Sep 17, 2007, 12:56:06 AM9/17/07
to cfau...@googlegroups.com
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?

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.

Mark Mandel

unread,
Sep 17, 2007, 1:00:30 AM9/17/07
to cfau...@googlegroups.com
Chris - What is populating the Struct?

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?

--
E: mark....@gmail.com
W: www.compoundtheory.com

camero...@gmail.com

unread,
Sep 18, 2007, 8:03:52 AM9/18/07
to cfaussie
Hi Chris
I've never quite understood why people use:

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

Steve Onnis

unread,
Sep 18, 2007, 8:06:27 AM9/18/07
to cfau...@googlegroups.com
structFind is used to determine is a structure has a key

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

Raymond Camden

unread,
Sep 18, 2007, 10:59:38 AM9/18/07
to cfau...@googlegroups.com
structKeyExists should be used to determine if a struct has a key.

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

Adam Cameron

unread,
Sep 18, 2007, 3:06:38 PM9/18/07
to cfaussie
> If you try and output "struct[key]" and the key doesn't exists that it will
> error

Sure. As with structFind().

Hence my question.


> I think maybe you have confused the meaning of your message :)

Maybe. Or maybe... not.

--
Adam

Mark Mandel

unread,
Sep 18, 2007, 7:08:08 PM9/18/07
to cfau...@googlegroups.com
I use StructFind() a lot - simply because I tend to encapsulate
struct's behind getter's and setters quite regualrly.

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

Andrew Scott

unread,
Sep 18, 2007, 8:06:40 PM9/18/07
to cfau...@googlegroups.com
This has been a very interesting thread.

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.

Maximilian Nyman

unread,
Sep 18, 2007, 8:59:26 PM9/18/07
to cfau...@googlegroups.com
I tried this (with CF8):

<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

Andrew Scott

unread,
Sep 18, 2007, 10:08:01 PM9/18/07
to cfau...@googlegroups.com
That is very interesting, I would actually say that this is a bug then.

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.

Barry Beattie

unread,
Sep 18, 2007, 10:29:42 PM9/18/07
to cfau...@googlegroups.com
"The reason being is that Java, as I see it when defining something as NULL
is telling you that it actually doesn't exist"

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.

Andrew Scott

unread,
Sep 18, 2007, 10:51:54 PM9/18/07
to cfau...@googlegroups.com
Barry,

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.

Raymond Camden

unread,
Sep 19, 2007, 3:07:55 PM9/19/07
to cfau...@googlegroups.com
Hey Mark - this is pretty interesting. This is the first time I've
used a real use for structFind. Do you mind if I blog this? Or if you
do - let me know and Ill link to it.

Mark Mandel

unread,
Sep 19, 2007, 8:28:49 PM9/19/07
to cfau...@googlegroups.com
Ray,

Go for it, I don't mind at all :oD

Mark

Chris Velevitch

unread,
Sep 19, 2007, 10:44:41 PM9/19/07
to cfau...@googlegroups.com
On 9/17/07, Mark Mandel <mark....@gmail.com> wrote:
>
> Chris - What is populating the Struct?

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.

Adam Cameron

unread,
Sep 20, 2007, 2:25:55 PM9/20/07
to cfaussie
This technique of yours, Mark, is a bit grim in my view (and it's just
that: my opinion only).

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

Mark Mandel

unread,
Sep 20, 2007, 7:59:16 PM9/20/07
to cfau...@googlegroups.com
>
> This technique of yours, Mark, is a bit grim in my view (and it's just
> that: my opinion only).
>
> It's only relevant if one doesn't know the key name already, as one
> CAN do this, quite happily:

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

Reply all
Reply to author
Forward
0 new messages