http://coldfusion.com logo
#cfml-general
Title
# cfml-general
r

risto

04/08/2022, 4:31 PM
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

Daniel Mejia

04/08/2022, 4:37 PM
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

risto

04/08/2022, 4:43 PM
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

Daniel Mejia

04/08/2022, 4:44 PM
your example is simple. try both recursion and breath-first.
r

risto

04/08/2022, 4:48 PM
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

Daniel Mejia

04/08/2022, 4:52 PM
it will be an easy exercise - your example is a good first issue for recursion
s

Scott Steinbeck

04/08/2022, 8:02 PM
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

risto

04/08/2022, 8:12 PM
@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

Daniel Mejia

04/08/2022, 8:13 PM
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

risto

04/08/2022, 8:15 PM
@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

Daniel Mejia

04/08/2022, 8:18 PM
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

Scott Steinbeck

04/08/2022, 8:18 PM
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

risto

04/08/2022, 8:22 PM
Nice
b

bdw429s

04/09/2022, 1:09 AM
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.