Code to determine whether a structure element exists and is populated

Hello CFExperts,

I have a structure, each item comprised of 4 elements, as represented below.

1st
#myStructure[1][1][1]# <!--- references 1st main-loop --->
#myStructure[1][1][2]# <!--- references 1st sub-loop --->
#myStructure[1][1][3]# <!--- associated data element --->
#myStructure[1][1][4]# <!--- another associated data element --->

2nd
#myStructure[1][2][1]# <!--- references 1st main-loop --->
#myStructure[1][2][2]# <!--- references 2nd sub-loop --->
#myStructure[1][2][3]# <!--- associated data element --->
#myStructure[1][2][4]# <!--- another associated data element --->

3rd
#myStructure[2][1][1]# <!--- references 2nd main-loop --->
#myStructure[2][1][2]# <!--- references 1st sub-loop --->
#myStructure[2][1][3]# <!--- associated data element --->
#myStructure[2][1][4]# <!--- another associated data element --->


The number of items that will be returned by the main-loop is unknown, and the number of items in each sub-loop is unknown.

I need to verify whether the next expected structure actually exists, prior to performing actions on it.

What code would verify if #myStructure[3][1][1]# actually exists?

These do not work:
<cfif StructKeyExists(structure,"#myStructure[1][1][1]#")>
<cfif StructKeyExists(structure,"#myStructure#")>
<cfif StructKeyExists(myStructure,#myStructure[1][1][1]#)>
<cfif StructKeyExists(myStructure,"myStructure[1][1][1]")>
<cfif StructKeyExists(myStructure,"#myStructure[1][1][1]#")>
<cfif StructKeyExists(structure,"#myStructure[1][1][1]#")>
<cfif IsDefined("#myMSDSSKU[1][1][1]#")>

I realize the short answer may be "<cfif StructKeyExists(myStructure, key)>", but I can't accept that answer unless you can clearly explain what the "key" is and display the code to reproduce it for this specific example.

Thanks in advance!

Tom
LVL 2
PierceWebAsked:
Who is Participating?
 
_agx_Connect With a Mentor Commented:
I realize the above is just an example, but structures don't really have an "order" like an arrays do ie arr[1], arr[2], etc...  Is your input really comprised of all nested structures or is it a combination of structures and arrays?
0
 
PierceWebAuthor Commented:
@ _agx_ You're right. I may really only be working with an array. Let me look into this some more and get back to you. Thanks.
0
 
_agx_Commented:
Sounds good.  I'll keep an eye out for your response.
0
 
PierceWebAuthor Commented:
@ _agx_ You are correct, sir!  I changed my code to put the data into an array, and can use the following to determine whether the next potentially available array item exists, (incrementing the [1][1] to account for the main loops and sub loops. Thanks.

<cfif #ArrayIsDefined(myStructure[1][1], 1)# EQ 'YES'>
0
 
_agx_Commented:
Glad it helped.  You could also trim it down to:

        <cfif ArrayIsDefined(myStructure[1][1], 1)>
              ... do something ...
        </cfif>

But that's mostly stylistic preference :)
0
All Courses

From novice to tech pro — start learning today.