Solved

Code to determine whether a structure element exists and is populated

Posted on 2014-10-31
5
116 Views
Last Modified: 2014-10-31
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
0
Comment
Question by:PierceWeb
  • 3
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
_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?
0
 
LVL 2

Author Comment

by:PierceWeb
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.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40416620
Sounds good.  I'll keep an eye out for your response.
0
 
LVL 2

Author Comment

by:PierceWeb
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'>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 40416640
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
The viewer will learn how to dynamically set the form action using jQuery.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now