Skip to main content
Inspiring
April 23, 2006
Answered

Dynamic Nested Structs

  • April 23, 2006
  • 9 replies
  • 1739 views
I'm looking for a way to build a dynamic nested structure in cfscript using a for loop. Something like:
<cfset MyStruct = StructNew()>
<cfset MyStruct["A"] = StructNew()>
<cfset MyStruct["A"]["B"] = StructNew()>
<cfset MyStruct["A"]["B"]["C"]= StructNew()>
<cfset MyStruct["A"]["B"]["C"]["D"]= StructNew()>
<cfset MyStruct["A"]["B"]["D"]["R"]= StructNew()>

The depth of the nested structure is unknown, 2 to 1000 and the A, B, C, D, R are all unknown. I have no problem creating the first struct MyStruct["A"], but can't seem to add the second + nested struct to the first dynamically in my loop. Seems like I want to concatenate each nested structure to the previous structure as I go.

Any thoughts?

<cfdump var="#MyStruct#">
This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer BSterner
I'm trying to build a nested structure of all names in a database. I'd guess that I'll have 1 root called root, then probably A-Z nested structs within the root. Then A-Z structs in all the first level A-Z structs and so on, this would probably go 50 deep.

The keys would be A-Z if there was a matching name with a letter at that position, probably not a lot of Z's as the second letter of someones name, so we would skip Z's at that level.

So now if you're name started with D, you would start at the struct root["D"]. Now root["D"] is our root so to speak, but now we are looking for the second letter in your name "A", so we would be at root["D"]["A"] ... so in the end I'd be at root["D"]["A"]["V"]["E"].

Now when I got to root["D"]["A"]["V"] of the struct, I'd also see all the other variations from there, ie root["D"]["A"]["V"]["I"]["D"]

Hmm...interesting, uh, task. Anyways, is something like this what you're after. One thing to note, I added a "endOfName" flag, to indicate if there was a name "Dan" and "Daniel" or "Fred" and "Fredrick".

9 replies

Inspiring
April 23, 2006
One last thing. To avoid redundant traversal of your structure for duplicate names, you may want to do a SELECT DISTINCT in your original query, or, if you need every individual record, do a QoQ, just select the name and use this for building your structure. Depending on the number of duplicates and length of the names, the overhead associated with doing an extra query may or may not save you execution time.

Most likely, it will.
Inspiring
April 23, 2006
Neo Rye wrote:
> I'm looking for a way to build a dynamic nested structure in cfscript using a

<cfscript>
root=structNew();
for (i=1; i LTE 10; i=i+1) {
// or use some other naming scheme for the nested structs
root["#i#"]=structNew();
root["#i#"].a=i;
root["#i#"].b=i*10;
root["#i#"].c=i*100;
}
</cfscript>

<cfdump var="#root#">
Neo RyeAuthor
Inspiring
April 23, 2006
Not quite. Your's only creates one nested struct. Mine needs to be more like a nested tree.
A
B B
C C C
D D D D


<cfscript>
root=structNew();
for (i=1; i LTE 10; i=i+1) {
// or use some other naming scheme for the nested structs
root["A"]=structNew();
root["A"]["B']=structNew();
root["A"]["B"]["C"]=structNew();
... and on and on. but without having to hard code it. What if I have 15 nested structs? It would look something like:
root["?"]["?"]["?"]["?"]["?"]["?"]["?"]["?"]["?"]["?"]["?"]["?"]["?"]["?"]["?"]=structNew();
...and I'd have to create 15 individual structs leading up to it.

}
</cfscript>
<cfdump var="#root#">

Dump this and you'll see what I need it to look like:
<cfset MyStruct = StructNew()>
<cfset MyStruct["A"] = StructNew()>
<cfset MyStruct["A"]["B"] = StructNew()>
<cfset MyStruct["A"]["B"]["C"]= StructNew()>
<cfset MyStruct["A"]["B"]["C"]["D"]= StructNew()>
<cfset MyStruct["A"]["B"]["D"]["R"]= StructNew()>
<cfdump var="#MyStruct#">