I need the help of someone way smarter than me. Tr...
# cfml-general
a
I need the help of someone way smarter than me. Trying to convert a flat array to a tree array but the way the structure is stored is a bit quirky and has me stumped. More in thread...
z
Structkeytranslate
a
Hopefully this example explains what data I have to start with, what I'm trying get and how far I've got 🙂 Any help very much appreciated! https://trycf.com/gist/c42edfb5e70b46c2a1dd0d597dc7606b/lucee5?theme=monokai
The
path
key defines the structure, the
org
key is just there to try and make sense of it when working on it!
Thanks Zac - cool function but don't think it helps here sadly
a
@Simone if you wan to see how to ask a question well... this is a good example.
@aliaspooryorik is the sort order of the org guaranteed? ie: it'll def be in that helpful order?
p
Ask AI to do it 😂
a
Oh I presume he wanted a correct answer, Patrick.
😎 1
a
@Adam Cameron good question. Hmm. I think so, but you've put doubt in my mind now! This is coming from an external source.
a
looks easy enough to sort first anyhow
a
I figured it could be done with some recursion but just brain fog after that
a
that's kinda what I'm doing. But still with
reduce
. ALthough that said four beers and 10:30pm is not helping. Nor is trycf.com
🍻 1
a
Not to worry - I appreciate you trying 🙂 I'll revisit it in the morning so sit back any enjoy your 5th beer Adam 🍻
m
If you didn't require it to be an array this is what i had done... https://trycf.com/gist/56fd8c323ae14bec7a854c3f479b653b/lucee5?theme=monokai
a
Thanks, closer than I got. I do need it to be an array though unfortunately.
Well, I started again from scratch and think I have a rough and ready solution... https://trycf.com/gist/03d1dbf301088eec25fda2718ab93ee8/lucee6-beta?theme=monokai
s
function toTree(data, level=1, parentNode=''){
var ChildLevel = level + 1;
returnData = data.filter((item)=>{return (listlen(<http://item.org|item.org>,"->") eq level and (level eq 1 OR find(parentNode, <http://item.org|item.org>)) )});
returnData = returnData.map((item)=>{
structappend(item, {children:toTree(data, ChildLevel, <http://item.org|item.org>)} )
return item
})
return returnData
}
actual = toTree(data)
a
Thanks @s1deburn Sadly the path key is what defines the actual structure, the org key is just there to try and make sense of it when working on it!
Hence why I've really struggled it 🙂
s
hmm would it still work switching the logic to use path? you would just need to identify the root nodes i guess?
a
@aliaspooryorik is this path data coming from an external source, or are you contriving it? There seems to be a slight incongruity in the data. Each element of the path (if one ignores the trailing zeros) is the insertion index of the immediate ancestor array, and the indexes are 1-based. except for the top level, where they're 0-based. This seems... incongruous (and makes the implementation more difficult). To me the data structure would be more consistent if the indexes were 1-based (not least of all because it works better with CFML), and
0
meant "no further levels". Or even if everything was 0-based, and maybe -1 was used for "no further levels". This is not a show stopper, but it means the solution algorithm needs to dick around normalising the data before using it.
Anyway, here you go: https://trycf.com/gist/d1d351715580345b3ca5cac93f8c886b/lucee5?theme=monokai Only tested with your data, and it makes the assumption that the source array is sorted such that ancestors always appear before their descendants. The trick with the main reduce is that you are not operating on the
tree
, you are operating at the place in the tree indicated by the
path
(if that makes sense). Can elaborate more / improve the code if it's unclear. Thanks for the Sat afternoon puzzle.
Slightly tidier CF-only version: https://trycf.com/gist/7870f4be41b7955d18127474feb9238f/acf2021?theme=monokai It give an explanatory label to the code that extracts usable path info parts from the
path
array. Lucee doesn't support destructuring assignments as yet, and it makes sense to use it here.
s
https://trycf.com/gist/bcadd99f55c7e750c71f29345e6d07de/lucee5?theme=monokai Updated to work off path. I hadn't looked at that solution from this morning prior. But looking at it now, it looks like a similar approach. Oh well.
a
Yeah the data is coming from an external source - I sure as heck wouldn't design it that way!
Thank you both very much for taking on the challenge. Much appreciated and hope you got some joy out of solving it. Thank you.
👍 1
b
Use XML.
🤔 1
a
Yeah @aliaspooryorik. USE XML. Big dummy.
a
What was I thinking? 🤦