Hi, I'm getting hierarchical data and would like t...
# cfml-general
r
Hi, I'm getting hierarchical data and would like to loop through it. I don't think I will get more than one nested level of data and I wrote the following code that will work for up to 2 nested levels. I'm interested in hearing if what I wrote is okay or there are better ways to write this code. Thanks https://trycf.com/gist/65e57bf1afb3991a73fa2171cbbf8239/lucee5?theme=monokai
d
Look into recursion functions, which is I think next best thing you can do with a little refactoring.
Write a recursion function and post it here. Give it a try.
r
I looked at some old tree code from about 15 years ago and couldn't grasp the recursion code. I also read this from Ban talking about simpler than the recursive algorithm. https://www.bennadel.com/blog/3750-replacing-depth-first-recursion-with-a-breadth-first-while-loop-in-lucee-cfml-5-3-3-62.htm
d
your example is simple. try both recursion and breath-first.
r
I guess when I wrote this I was just looking for the most simplistic version. I will just be updating the order of a list. This weekend I should have time to try a recursive version.
d
it will be an easy exercise - your example is a good first issue for recursion
s
maybe its too soon to give the answer, but i did this one for fun so here is a sample of how you could display your data structure with recursion
Copy code
function printTree(data, indent="", result=""){
    for (child in data) { // First level
        if(len(indent)) result &= indent & "└"; //only add the indent if a child
        
        result &= child.id & "<br>"; //print id with a break
        
        if(structKeyExists(child,"children")) //if there are children recurse with a deeper indent
            result &= printTree(child["children"], indent & "&nbsp;&nbsp;"); 
    }
    return result;
}
echo(printTree(apidata));
👍🏾 1
r
@Scott Steinbeck Scott this is great! TBH I was a little intimidated with recursion. As a matter of fact it's the exact opposite of intimidating after seeing your example. Much appreciated
👍 1
d
I told you it was simple
Ben Nadel's is talking about building the data structure as a tree structure. You already have a tree structure. Plus his example of breadth first loop uses recursion as well.
r
@Daniel Mejia 😅 I guess after seeing scotts example but that's not what was going through my mind at first after looking at these trees https://www.sparknotes.com/cs/recursion/examples/section5/
I learned a lot today. Thanks!
d
Yeah I get the tree figures in this article makes it seem like super advanced algorithm soltuions. But it just depends on how complex your edge cases are.
s
In order to think about recursion, you just need to think about the next step. loop over each item in the array does the item have a child? if so, call the same function on the child array the crucial part is keeping all the data together, you will notice in my example
Copy code
result &= printTree(child["children"], indent & "&nbsp;&nbsp;");
i am adding the result of the child onto the parent’s string. this same thing can be done with other types (array, struct, etc.) once your finished adding everything on, you return the whole thing as one big tree and display it
r
Nice
b
The cool thing about recursion us it usually ends up being simpler than you think it will be. Each invocation only needs to worry about processing one node in the tree, delgating to itself for the children nodes, and returning the results. It usually seems more complicated if you're trying to solve more than that.