newtoperl101 has asked for the wisdom of the Perl Monks concerning the following question:
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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Recursive program
by roboticus (Chancellor) on Aug 22, 2014 at 18:06 UTC | |
|
Re: Recursive program
by remiah (Hermit) on Aug 24, 2014 at 02:43 UTC |