# Recursion and lists - Coldfusion - Advanced Techniques

Hey folks, I have a problem to which I'm fairly certain the answer would be a recursive function, but I'm having trouble coming up with the algorithm. I'm willing to see if the community at large might be able to help me get my head around this one. I've got a piece of javascript which generates dropdown menus with as many sublevels as I like. The author of that code generates the menus based on a set of arrays that the user would need to specify. For example: Menu1 = new Array("argument1","argument2","argument3","argumen t4","argument5","argument6"); Menu2 = new Array("argument1","argument2","argument3","argumen t4","argument5","argument6"); Menu2_1 = ...

1. ## Recursion and lists

Hey folks,

I have a problem to which I'm fairly certain the answer would be a recursive
function, but I'm having trouble coming up with the algorithm. I'm willing to
see if the community at large might be able to help me get my head around this
one.

I've got a piece of javascript which generates dropdown menus with as many
sublevels as I like. The author of that code generates the menus based on a set
of arrays that the user would need to specify. For example:
Array("argument1","argument2","argument3","argumen t4","argument5","argument6");

Array("argument1","argument2","argument3","argumen t4","argument5","argument6");

Array("argument1","argument2","argument3","argumen t4","argument5","argument6");
Array("argument1","argument2","argument3","argumen t4","argument5","argument6");

Array("argument1","argument2","argument3","argumen t4","argument5","argument6");
Array("argument1","argument2","argument3","argumen t4","argument5","argument6");
Array("argument1","argument2","argument3","argumen t4","argument5","argument6");

Array("argument1","argument2","argument3","argumen t4","argument5","argument6");
Array("argument1","argument2","argument3","argumen t4","argument5","argument6");

Array("argument1","argument2","argument3","argumen t4","argument5","argument6");

... on and on like that. For the sake of discussion, what exactly the
arguments are doesnt' matter.
My goal is to write a function that will generate the javascript code (like
the above) from six lists (one for each argument). I *think* this is best done
with a recursive function, because I want it to work with any number of
sublevels.

My lists currently look something like this:
A|B~1~2~3+a+b+c^1a^2a~4|C~1~2|D~1|E

where:
the pipe delimiter represents the oldest generation in the hirearchy
the tilde delimiter represents the next oldest generation
the + is the next oldest...
etc.

My *actual* delimeters are chr(1488) - chr(1497) and are assigned to the
variables d0 - d9. I fugure ten levels is more than I'm ever likely to need.

I'm not sure how to attack this problem. I've written one recursive function
in my life and that was the factorial function that everybody writes or learns
at some time in their life. I'm not even sure if I should pass all six lists
into the function or if I should include them or treat them as global... I
don't know.

I sure would appreciate any advice I can get on how to attack this problem, or
how it might be solved recursively. If I knew ahead of time that I was only
going to allow three or even four generations of menus, then I would just hard
code three or four nested loops and be done with it. But I'd like this to be as
flexible as possible so that the other developers in my team working on other
various systems, could use the same menuing system, simply by writing and
maintaining some lists and calling a function.

What do you think, am I crazy to think this is possible or what? :)

g1zm0guy Guest

2. ## Re: Recursion question

No thoughts yet? hmmm...
(bump)
g1zm0guy Guest

3. ## Re: Recursion question

First of all I would suggest using an array of structures to represent your
menu rather than a list with several delimiters as it is terribly difficult to
follow. I have outlined the code I would use in the attached code box. This
uses an array of structs, each struct has two keys, one is sLabel which is the
label for the menu on screen, the other is aMenuChild now this is a
one-dimensional array that may contain more child menu items. So to use a
recursive function to 'unpack' this array, you would do something along the
lines of using arrayLen() on aMenuChild if this is not empty then loop thourgh
the array and recurse this dpwn outputting your javascript as you go.

HTH

<cfscript>

// this is the first menu item

// this is the first menu items children

// this is the first menu items grand children
Children Label";

Stressed_Simon Guest

4. ## Re: Recursion question

Simon,

Thanks for being the first to respond. :)

I thought originally of using arrays of structures, but thought lists might be
problem, and just this morning (5:30a... *sigh*), thought I had a solution, but
it must have been a dream. :o

Anyway, I'm glad you brought up arrays of structs. I see now that this
solution will also solve my naming problem as:
..onclick, or.NumberOfSubs, etc.). This is probably the way I'm going to have to
go, as I've spent almost all of my Thanksgiving holiday trying to come up with
a solution using lists. The arrays will be just as easy to maintain, and might
make more sense as well.

Now I just have to write the routine to "unpack" them. :)

Thanks.

g1zm0guy Guest

5. ## Re: Recursion question

Just a note, if you are going to use recursion you will need to make the array
names the same or you will have to use for-in loops and isArray() which is just
messy. So I would do something like:-

rather than

Stressed_Simon Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•