Professional Web Applications Themes

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. #1

    Default 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:
    Menu1 = new
    Array("argument1","argument2","argument3","argumen t4","argument5","argument6");

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

    Menu2_1 = new
    Array("argument1","argument2","argument3","argumen t4","argument5","argument6");
    Menu2_2 = new
    Array("argument1","argument2","argument3","argumen t4","argument5","argument6");

    Menu2_2_1 = new
    Array("argument1","argument2","argument3","argumen t4","argument5","argument6");
    Menu2_2_2 = new
    Array("argument1","argument2","argument3","argumen t4","argument5","argument6");
    Menu2_2_3 = new
    Array("argument1","argument2","argument3","argumen t4","argument5","argument6");

    Menu2_3 = new
    Array("argument1","argument2","argument3","argumen t4","argument5","argument6");
    Menu2_4 = new
    Array("argument1","argument2","argument3","argumen t4","argument5","argument6");

    Menu3 = new
    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. #2

    Default Re: Recursion question

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

  3. #3

    Default 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>
    // initialise menu
    aMyMenu = arrayNew(1);

    // this is the first menu item
    aMyMenu[1] = structNew();
    aMyMenu[1].sLabel = "First Menu Item Label";
    aMyMenu[1].aMenuChild = arrayNew(1);

    // this is the first menu items children
    aMyMenu[1].aMenuChild[1] = structNew();
    aMyMenu[1].aMenuChild[1].sLabel = "First Child Menu Item Label";
    aMyMenu[1].aMenuChild[1].aMenuChild = arrayNew(1);

    aMyMenu[1].aMenuChild[2] = structNew();
    aMyMenu[1].aMenuChild[2].sLabel = "Second Child Menu Item Label";
    aMyMenu[1].aMenuChild[2].aMenuChild = arrayNew(1);

    aMyMenu[1].aMenuChild[3] = structNew();
    aMyMenu[1].aMenuChild[3].sLabel = "Third Child Menu Item Label";
    aMyMenu[1].aMenuChild[3].aMenuChild = arrayNew(1);

    // this is the first menu items grand children
    aMyMenu[1].aMenuChild[1].aMenuChild[1] = structNew();
    aMyMenu[1].aMenuChild[1].aMenuChild[1].sLabel = "First Child Menu Item's
    Children Label";
    aMyMenu[1].aMenuChild[1].aMenuChild[1].aMenuChild = arrayNew(1);
    </cfscript><cfdump var="#aMyMenu#">

    Stressed_Simon Guest

  4. #4

    Default Re: Recursion question

    Simon,

    Thanks for being the first to respond. :)

    I thought originally of using arrays of structures, but thought lists might be
    easier to maintain. However, I've been waking up nights thinking about this
    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:
    Menu1_1_2 would be RootMenu[1].ChildMenu[1].GrandChildMenu[2].menuTitle (or
    ..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. #5

    Default 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:-

    RootMenu[1].ChildMenu[1].ChildMenu[2].menuTitle

    rather than

    RootMenu[1].ChildMenu[1].GrandChildMenu[2].menuTitle

    Stressed_Simon Guest

Similar Threads

  1. Recursion
    By Jeff Westman in forum PERL Beginners
    Replies: 15
    Last Post: November 4th, 12:18 AM
  2. recursion for building up lists seems to be slow
    By oliversa webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 0
    Last Post: September 22nd, 10:58 AM
  3. lists, attaching behaviour dynamiclly, update lists
    By crazy big fun in forum Macromedia Director Basics
    Replies: 0
    Last Post: June 28th, 06:20 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139