DISCLAIMER: I have not tried this code out, but I think it will work. All I know is that :perl -wc doesn't complain. Let me know what happens.

If it works I will be happy, because I've been away from Perl for a long time, and I'm trying to shake off some rust. Even if it doesn't work, it's got to be close. Anyway:

$tree = GetCategories($node)

# # # # # # #' . . `# # i # making an ass out of u and me since 1975 # assumptions # #+_. ._+# my %sql; # is filled w/ names of tables that hold category data my $dbh; # database handle =head2 The Structure of a Node { id => $number, name => $string, children => $array_ref_of_more_nodes, } =head2 Usage To get the whole tree: my $tree = GetCategories(); To get a partial tree, pass in a hashref that has an C<id> key representing the parent C<category_id> to start with: my $subtree = GetCategories($starting_node); =cut sub GetCategories { my $node = shift || undef; my $select_children = "SELECT category_id,name FROM $sql{categories} "; # Create the root node only on the 1st time through, # and complete the SQL statement for # selecting all the nodes children. if (not defined $node) { $node = { id => 0, name => '/', children => [ ], }; $select_children .= "WHERE parent IS NULL"; } else { $select_children .= "WHERE parent=$node->{id}"; } # Find all the children of the current node. my $all_cats = $dbh->selectall_arrayref($select_children); my $c; # Add each child to $node->{children}, and # then recursively add get each child's children foreach $c (sort { $a->[1] cmp $b->[1] } @{$all_cats}) { my $child_node = { id => $c->[0], name => $c->[1], children => [ ], }; # ............. depth-first style ..... push @{$node->{children}}, $child_node; GetCategories($child_node); } return $node; }

In reply to Re: Collecting data in a recursive routine. by beppu
in thread Collecting data in a recursive routine. 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.