Using structKeyExists for nested objects

I have a nested structure like struct1.struct2.foo

. I would like to check if foo

. However, it is struct2

also not guaranteed. I don't want to use isDefined()

, but I also think that calling structKeyExists()

twice is wasteful (e.g.if (structKeyExists(struct, 'struct2') && structKeyExists(struct.struct2, 'foo')) {}

I thought about using structFindKey()

, but then I don't want to face the problem if existsstruct1.foo

Is there a better way to do this?

This is a similar question to this question , but I am not dealing with an XML document, so most of the answers in this post do not work for me.

+4


source to share


3 answers


isDefined () can potentially return incorrect results. This is not as accurate as structKeyExists (). And unless you repeat this code a thousand times in pop, you won't notice a performance difference. They both work very well. But if the key doesn't exist, that could make a difference (again, only with thousands of iterations). isDefined () will go through the various available scopes if it can't find it on the first pass. Even if you passed it a variable that looked like it indicated the scope you wanted to check. structKeyExists () is pretty clear when it looks for that key. If it doesn't find it, it will stop and return FALSE. It may seem like you are doing unnecessary work, but this returns a much more accurate result. And reading the code gives you a pretty good idea of ​​what you are looking for,so you don’t have to worry about it not being clean.



Peter's offer was pretty good. Check this link.

+2


source


Nothing wrong with using it isDefined("struct1.struct2.foo")

. It's not as awfully slow as you might think. Start with an area if you want to make it a little faster, for example"variables.struct1.struct2.foo"



ColdFusion 9 CFML Reference (PDF)

+5


source


This is the same question (albeit more succinct) underlying the earlier question:

How to dynamically iterate over an array of structures

I would suggest the same answer.

How to dynamically iterate over an array of structures

To recap the important part, the following function should do what you want:

 <cffunction name="StructGetByKeyList">
      <cfargument name="struct">
      <cfargument name="key">

      <cfif StructKeyExists(struct,ListFirst(key,"."))>
           <cfif ListLen(key,".") GT 1>
                <cfreturn StructGetByKeyList(struct[ListFirst(key,".")],ListRest(key,"."))>
           <cfelse>
                <cfreturn struct[key]>
           </cfif>
      <cfelse>
           <cfreturn "">
      </cfif>
 </cffunction>

      

Then you can simply call StructGetByKeyList (struct1, "struct2.foo") and it will return a string for the key if it exists and an empty string if it doesn't.

To use the following instead of boolean:

<cffunction name="StructNestedKeyExists">
    <cfargument name="struct">
    <cfargument name="key">

    <cfif StructKeyExists(struct,ListFirst(key,"."))>
        <cfif ListLen(key,".") GT 1>
            <cfreturn StructNestedKeyExists(struct[ListFirst(key,".")],ListRest(key,"."))>
        <cfelse>
            <cfreturn true>
        </cfif>
    <cfelse>
        <cfreturn false>
    </cfif>
</cffunction>

      

+4


source







All Articles