?
Solved

Accessing a structure of array...

Posted on 2004-12-01
6
Medium Priority
?
177 Views
Last Modified: 2013-12-24
How can I best access the innermost array text in a structure like the one below? It is a structure which contains an array, which contains another structure, which contains another array. A sample loop would be welcome:

struct
Errata
  array
  1
    struct
    Text
       array
       1 Guests wanting to book a walking tour
       2 must speak to their representative
       3 locally. For any guests staying 7 nights
       4 or less will not be able to take part
       5 in the Tuesday tour. Picnics are
       6 available and bookable locally at an
       7 additional charge.

Many thanks,
MJ
0
Comment
Question by:mjacobs2929
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Dain_Anderson
ID: 12719183
This almost looks like an array that has a structure, which then has an array. To produce (at least what I *kind of* think you've got) it:

<CFSET Errata = ArrayNew(1)>
<CFSET Errata[1] = StructNew()>
<CFSET Errata[1].Text = ArrayNew(1)>
<CFSET Errata[1].Text[1] = "Guests wanting to book a walking tour">
<CFSET Errata[1].Text[2] = "must speak to their representative">
<CFSET Errata[1].Text[3] = "locally. For any guests staying 7 nights">
<CFSET Errata[1].Text[4] = "or less will not be able to take part">
<CFSET Errata[1].Text[5] = "in the Tuesday tour. Picnics are">
<CFSET Errata[1].Text[6] = "available and bookable locally at an">
<CFSET Errata[1].Text[7] = "additional charge.">

If this is the case, to access the innermost array, you can use the syntax:

<CFOUTPUT>
<CFLOOP FROM="1" TO="#ArrayLen(Errata[1].Text)#" INDEX="i">
    #Errata[1].Text[i]#<BR>
</CFLOOP>
</CFOUTPUT>

To loop through the entire things, you could wrap that within another loop:

<CFOUTPUT>
<CFLOOP FROM="1" TO="#ArrayLen(Errata)#" INDEX="j">
      <CFLOOP FROM="1" TO="#ArrayLen(Errata[j].Text)#" INDEX="i">
          #Errata[1].Text[i]#<BR>
      </CFLOOP>
</CFLOOP>
</CFOUTPUT>

There are a lot of other was to play with arrays and structs, but hopefully this gets you started.

-Dain
0
 
LVL 9

Accepted Solution

by:
Dain_Anderson earned 2000 total points
ID: 12719196
Oops, the bottom loop should say:

<CFOUTPUT>
<CFLOOP FROM="1" TO="#ArrayLen(Errata)#" INDEX="j">
     <CFLOOP FROM="1" TO="#ArrayLen(Errata[j].Text)#" INDEX="i">
         #Errata[j].Text[i]#<BR>
     </CFLOOP>
</CFLOOP>
</CFOUTPUT>

(the '1' should have read 'j')
0
 
LVL 5

Expert Comment

by:umbrae
ID: 12719265
Main trouble here is that if you want to automatically get to the deepest point of the array/struct structure, the fieldname of the structs inside must be known. For example, if thye fieldname was consistently 'errata', and there was consistently only one element in the array, it would be something like this:

<cfset mainStruct = StructNew()>
<cfset mainStruct.errata = ArrayNew(1)>
<cfset mainStruct.errata[1] = StructNew()>
<cfset mainStruct.errata[1].errata = ArrayNew(1)>
<cfset mainStruct.errata[1].errata[1] = 'Blah!'>

<cfset curStruct = mainStruct>
<cfloop condition="#isArray(curStruct['errata'])#">
<cfif not isStruct(curStruct['errata'][1])>
  <cfset successArr = curStruct['errata']>
  <cfbreak>
<cfelse>
  <cfset curStruct = curStruct['errata'][1]>
</cfif>
</cfloop>

<cfdump var="#successArr#" label="the right one">


Hope this helps

-Umbrae
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:umbrae
ID: 12719299
Bah. I realized I just overcomplicated things by assuming you meant you wanted to find the innermost array regardless of depth. My apologies.
0
 

Author Comment

by:mjacobs2929
ID: 12724522
Thanks Dain,

This seems to work (sorry, forgot to mention that 'Text' is always the innermost array):

<cfoutput>
 <cfloop from="1" to="#ArrayLen(StructKeyArray(ResultfromcfcGemini))#" index="i">
  <cfloop from="1" to="#ArrayLen(ResultfromcfcGemini.Errata)#" index="j">
       <cfloop from="1" TO="#ArrayLen(ResultfromcfcGemini.Errata[j].Text)#" index="k">
             #ResultfromcfcGemini.Errata[j].Text[k]#<br>
       </cfloop>
  </cfloop>
 </cfloop>
</cfoutput>


There can also be more than one array in 'Errata'.

Now I need to concat: #ResultfromcfcGemini.Errata[j].Text[k]#

Many thanks,
MJ
0
 
LVL 9

Expert Comment

by:Dain_Anderson
ID: 12727447
Groovy -- glad I could help!

-Dain
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The following information will get you familiar with your new DV server, including the (mt) Account Center, the Plesk Control Panel, our world-renowned support department and the rest of the (mt) tools that come with your new service.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Suggested Courses

621 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