Code to determine whether a structure element exists and is populated

Posted on 2014-10-31
Last Modified: 2014-10-31
Hello CFExperts,

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

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

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

#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!

Question by:PierceWeb
  • 3
  • 2
LVL 52

Accepted Solution

_agx_ earned 500 total points
ID: 40416532
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?

Author Comment

ID: 40416592
@ _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.
LVL 52

Expert Comment

ID: 40416620
Sounds good.  I'll keep an eye out for your response.

Author Comment

ID: 40416626
@ _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'>
LVL 52

Expert Comment

ID: 40416640
Glad it helped.  You could also trim it down to:

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

But that's mostly stylistic preference :)

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question