Hello folks, Can one of you Monkish gentlemen help me with this? I am stuck with a recursion issue here. Basically i am trying to print out a spreadsheet of parents and children of this structure:

A1

A1 B1

A1 B1 C1

A1 B1 C1 D1

A1 B1 C2

A1 B1 C3

A1 B1 C4 D2

A1 B2

A1 B2 C5 D3

Here B1 is child of A1 and C1 is child of B1. B1 has many children C1,C2,C3 C4. My program succeeds till line 4. The moment the last child doesn't have a grand child under it, it stops. How could I make it go back and keep on printing out till the end? Here is my actual code snippet: $top calls the first asset A1 which is parent of B1,B2 etc.
my @line; foreach (my $top = $top_sth->fetchrow_hashref) { $line[0]= $top->{guid}; push @line,$top->{title}; $exchange->stdxls_write($exchange->stdxls_row(), \@line); print STDERR "Level1 ".$top->{title}."\n"; child_levels($top->{asset_id},@line); } $workbook->close(); sub child_levels { my ($asset_id,@line) = @_; my $child_sth = $dbh->prepare(' SELECT asset_id ,title ,seq ,guid ,parent_id FROM asset WHERE parent_id = ? ORDER BY seq ') or die $dbh->errstr; $child_sth->execute($asset_id) or die $child_sth->errstr; foreach (my $child = $child_sth->fetchrow_hashref) { $line[0]= $child->{guid}; push @line,$child->{title}; $exchange->stdxls_write($exchange->stdxls_row(), \@line); child_levels($child->{asset_id},@line); }

In reply to Recursive program by newtoperl101

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.