views:

80

answers:

1

In ColdFusion, when I call IsDefined("root.L1[1].L2"), I am getting the following error:

Parameter 1 of function IsDefined, which is now root.L1[1].L2, must be a syntactically valid variable name.

This is a valid variable name, so what gives?

Here is my simplified test code:

<cfscript>
  root = StructNew();
  root.L1 = ArrayNew(1);
  root.L1[1] = StructNew();
  root.L1[1].L2 = "foo";

  WriteOutput("root.L1[1].L2 is: #root.L1[1].L2#<br/>"); //no exception

  if(IsDefined("root.L1[1].L2")) //exception!
    WriteOutput("It is defined!");
  else
    WriteOutput("It is not defined!");
</cfscript>
+11  A: 

Try

StructKeyExists(root.L1[1],"L2")

instead of isDefined()

I vaguely recall there being issues with complex variables with isdefined(), but I can't recall the version.

Stephen Moretti
but what if i don't know for sure that root.L1 has any elements? is there any alternative? I can check `IsDefined("a.b.c.d")` even if `b` doesn't have a `c` element, but i guess that doesn't work if there is an array in the structure.
Jenni
You might need to stack your checks with AND statements then. Check the array length [using something like arrayLen(root.L1) GT 0] before doing the final structKeyExists above.
Daniel Sellers