Hi, PMs. I am working on a minimally functional discussion forum that reads record/messages from a flatfile ASCII database. All the messages have relationships to other messages (i.e. parent and children messages), and I am trying to create a "tree" view for a list of messages. I sort of succeeded, but the messages are in the reverse order. I need help (in more ways than one at this point).

Each record has a record ID, a parent field, a space-delimited list of children for a field, and some message data fields. A function called &get_record takes a record id, and puts the record in a hash. I usually display records with a foreach loop.

The HTML display function for the tree view looks like:

# @ids are a list of record IDs to display. they're sorted by id beca +use newer messages have higher id numbers @sorted_ids = reverse sort { $a cmp $b } @ids; foreach (@sorted_ids) { # this function displays a tree for each id given. the function wil f +ollow. &do_tree ($_); } sub do_tree { # -------------------------------------------------------- # this is the tree loop that call itself to display records in # tree form. pass it some record ids and see what happens! my %rec = &get_record ($_[0]); my @children; my @kids = split (/ /, $rec{'Children'}); # get all the kids foreach $kid (@kids) { push (@children, $kid) if ($kid); } # remove +blanks if (@children) { # if kids then loop @sorted_children = sort { $a cmp $b } @children; print "<li>[$rec{'ID'}] $rec{'Subject'}<ul>\n"; foreach $child (@sorted_children) { # here, the function calls itself. is there a better way to do this o +r am i doing this wrong? &do_tree ($child); } print "</ul></li>\n"; } else { # no kids? actually display the record then print "<li>[$rec{'ID'}] $rec{'Subject'}</li>\n"; } }

When I take out the sorting code, things work. When the sorting code is in, the messages don't all display. I double-checked the database and the rest of my code, and I'm pretty sure the problem lies with how I wrote do_tree. I'm pretty stuck on this, and I've misplaced my Perl Cookbook. Any help would be greatly appreciated.

Currently, the forum can be found here.

- Leam


In reply to Recursion question... by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.