Code to determine whether a structure element exists and is populated

Posted on 2014-10-31
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 52

Accepted Solution

_agx_ earned 2000 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

770 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