Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

I have items ("leaves") that can belong to one or more groups("branchs"), and every branch can own one or more items.I need to store this kind of data in a table and print it in txt format.The items belonging to two or more branchs can be printed in a tree(or not printed in the tree).
my question is, which module should I select?There are so many tree module in cpan,i don't know which one fit me best.

thanks,

HP

Replies are listed 'Best First'.
Re: Tree module select
by abell (Chaplain) on Oct 13, 2002 at 14:39 UTC

    Even if one the favourite mantras here is "look for an existing module", in this case I would consider implementing my own solution. Depending on your constraints/priorities, you could implement each group as an array with references to the elements belonging to it, or add to each element an array of references to the groups it belongs to, or put all couples (\$group, \$element) in an array. It shouldn't take that long, and probably less than finding and customizing the module you are looking for.

    I'd be glad to suggest more specific solutions, but I have problems figuring out some elements from your post. For instance, do you have deeper nesting levels, is the table you are talking about a relational database table?


    Best regards

    Antonio

    The stupider the astronaut, the easier it is to win the trip to Vega - A. Tucket
      Thanks for the help
      My tree-like data may look like this:

      3160
        |---8500010
        |---8500020
        |      |----8500021
        |      |       |----8500121
        |      |       \----8500122
        |      |----8500022
        |      \----8500023
        |              |----8500024
        |              \----8500025
        |---8500021
        |       |---8500121
        |       \---8500122
        \---8500030
      3171
      |----2800010 |----2800020 | \----8500025 |----8500010 |----8500020 | |----8500021 | | |----8500121 | | \----8500122 | |----8500022 | \----8500023 | |----8500024 | \----8500025 \---8500023 |----8500024 \----8500025

      8500025 belong to 3160 and 3171
      8500020 belong to 3160 and 3171

      It can be printed as this:

      3160
        |---8500010
        |---8500020
        |---8500021
        \---8500030
      3171
      |----2800010 |----2800020 | \----8500025 |----8500010 |----8500020 \---8500023

      The table may like this:
      -------------------------
       parentid    |   iiem id
      -------------------------
                   |    3160
        3160       |  8500010
        3160       |  8500020
        8500020    |  8500021
        8500021    |  8500121
        8500021    |  8500122
        8500020    |  8500022
        8500020    |  8500023
        8500023    |  8500024
        8500023    |  8500025
        3160       |  8500021
        3160       |  8500030
                   |    3171
        3171       |  2800010
        3171       |  2800020
        2800020    |  8500025
        3171       |  8500010
        3171       |  8500020
        3171       |  8500023
      ---------------------------
      

      I don't know which data stucture i should use.
Re: Tree module select
by Revelation (Deacon) on Oct 13, 2002 at 16:40 UTC
    Unfortunately, I haven't really seen a valid system for such trees. Not many people plan to have a leaf coming off of more than one branch, but I do understand how this could be helpful. Therefore, I'd leave you with four possible solutions:

    • Build your own tree module that will do what you need it to do. (One idea I've always loved, and use in my own home-spun tree algorithms is the idea of a sort key, which can tell all the parents of a single id, and could possible allow for two parents. A sort key looks like (root_id,branch_id,parent_id,leaf_id), assuming one has a root, a branch a sub-branch, and a leaf. You could then hack into that allow for (root_id,branch_id,[parent_id|other_parent_id],leaf_id), and parse into that with a split, and a regex. This would be helpful if you want to get the history of a leaf, but developing this into a full-scale tree module would take some time.
    • Duplicate the leafs, and use DBIx::Tree (This means that you'd leave all the same information, except for the id, and parent id.) Unfortunately, it also means that you'll have to duplicate children of that parent.
    • Develop your own tree algorithm, or edit DBIx::Tree to allow for two sets of parents. (This actually wouldn't be too hard.) I would suggest this solution, or the next one. My advice would be to allow DBIx::Tree to search for an or clause in your parent_id, and then have that clause build to both parents. Unfortunately you'd have to change the algorithm a little further to allow for sub-leaves to be built in both places. I'll leave that as an exercise for your mind :)
    • Look some more for a cpan solution, because I don't know it :)

    Gyan Kapur
    gyan.kapur@rhhllp.com