Accessing a structure of array...

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
mjacobs2929Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dain_AndersonCommented:
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
Dain_AndersonCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
umbraeCommented:
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

umbraeCommented:
Bah. I realized I just overcomplicated things by assuming you meant you wanted to find the innermost array regardless of depth. My apologies.
0
mjacobs2929Author Commented:
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
Dain_AndersonCommented:
Groovy -- glad I could help!

-Dain
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.