Solved

Code to determine whether a structure element exists and is populated

Posted on 2014-10-31
5
125 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
[X]
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
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
QQ problem 22 87
powershell switch statement 7 46
Application.cfm not found issue 2 61
run powershell script endlessly 3 22
Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

738 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