Click here to get back home

a problem using Tree::DAG_Node?

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
a problem using Tree::DAG_Node? jnevans 12-09-2004
Posted by jnevans on December 9, 2004, 2:44 pm
Please log in for more thread options
I was programming with the Tree::DAG_Node perl module and I was able to
get it to work up to a point where it just dropped the ball or maybe I
am blind to what I am doing wrong.

I am doing a walkdown using callback down a DAG tree. When I hit a
certain node, I wanted to process the data further. The problem is
that when I insert a foreach statement, the walkdown stops traversing
the tree.

Testing the DAG Tree output:

print map "$_n" , @;
produces the following diagram so I know the Tree creation was
successful:
|
<Shipment and Tracking Data>
|
<2000886>
|
<INV1000>
/--------------
| |
<TrackUPS1234> <TrackUPS4321>

using the following code:

my $found_customer = 0;
$root->walk_down({
callback => sub {
my $node = shift;
print " " x $_[0]->;
if ( $_[0]-> eq 0 ) {
print "n" ;
}
elsif ( $_[0]-> eq 1 ) {
if ( $found_customer eq 1 ) {
print "write footer emailn";
print " " x $_[0]->;
}
print "header email - Customer# " . $node->name . " "
.. $node-> attributes->. " " . $node->attributes-> .
"n" ;
$found_customer = 1;
$f0 = "";
$f1 = "";
$f2 = "";
$f3 = "";
$f4 = "";
@customized_letter_lines = "";
$f0 = $node->attributes->;
$f1 = $node->attributes->;
$f2 = $node->name;

my @letter_lines = @header_lines;

}
elsif ( $_[0]-> eq 2 ) {
print "Invoice " . $node->name . "n";
$f3 = $node->name;
}
elsif ( $_[0]-> eq 3 ) {
print " Track " . $node->name . "n";
$f4 = $node->name;
}
else {
1
}

},
_depth => 0
});

if ( $found_customer eq 1 ) {
print " write footer emailn";
}

output is:

header email - Customer# 2000886 Happy, Inc. jd@dawnsign.com
Invoice INV1000
Track TrackUPS1234
Track TrackUPS4321
write footer email

so far so good, but when I add the foreach statement:

....
my @letter_lines = @header_lines;
#insert the foreach statement
foreach my $letter_line (@letter_lines) {
}
#end insert the foreach statement


The output is now:

header email - Customer# 2000886 Happy Inc. jd@dawnsign.com
write footer email

As you can see, it did not traverse the Invoice(INV) and Track nodes.

Can you shed some light on why it stopped traversing the rest of the
nodes after the foreach statement?

Much thanks,
Jared Evans



Posted by gmax on December 12, 2004, 3:39 pm
Please log in for more thread options
jnevans@gmail.com wrote:
> I was programming with the Tree::DAG_Node perl module and I was able to
> get it to work up to a point where it just dropped the ball or maybe I
> am blind to what I am doing wrong.
>
> I am doing a walkdown using callback down a DAG tree. When I hit a
> certain node, I wanted to process the data further. The problem is
> that when I insert a foreach statement, the walkdown stops traversing
> the tree.
>
> Testing the DAG Tree output:
>
> print map "$_n" , @;
> produces the following diagram so I know the Tree creation was
> successful:
> |
> <Shipment and Tracking Data>
> |
> <2000886>
> |
> <INV1000>
> /--------------
> | |
> <TrackUPS1234> <TrackUPS4321>
>
> using the following code:
>
> my $found_customer = 0;
> $root->walk_down({
> callback => sub {
> my $node = shift;
> print " " x $_[0]->;
> if ( $_[0]-> eq 0 ) {
> print "n" ;
> }
> elsif ( $_[0]-> eq 1 ) {
> if ( $found_customer eq 1 ) {
> print "write footer emailn";
> print " " x $_[0]->;
> }
> print "header email - Customer# " . $node->name . " "
> . $node-> attributes->. " " . $node->attributes-> .
> "n" ;
> $found_customer = 1;
> $f0 = "";
> $f1 = "";
> $f2 = "";
> $f3 = "";
> $f4 = "";
> @customized_letter_lines = "";
> $f0 = $node->attributes->;
> $f1 = $node->attributes->;
> $f2 = $node->name;
>
> my @letter_lines = @header_lines;
>
> }
> elsif ( $_[0]-> eq 2 ) {
> print "Invoice " . $node->name . "n";
> $f3 = $node->name;
> }
> elsif ( $_[0]-> eq 3 ) {
> print " Track " . $node->name . "n";
> $f4 = $node->name;
> }
> else {
> 1
> }
>
> },
> _depth => 0
> });
>
> if ( $found_customer eq 1 ) {
> print " write footer emailn";
> }
>
> output is:
>
> header email - Customer# 2000886 Happy, Inc. jd@dawnsign.com
> Invoice INV1000
> Track TrackUPS1234
> Track TrackUPS4321
> write footer email
>
> so far so good, but when I add the foreach statement:
>
> ...
> my @letter_lines = @header_lines;
> #insert the foreach statement
> foreach my $letter_line (@letter_lines) {
> }
> #end insert the foreach statement
>
>
> The output is now:
>
> header email - Customer# 2000886 Happy Inc. jd@dawnsign.com
> write footer email
>
> As you can see, it did not traverse the Invoice(INV) and Track nodes.
>
> Can you shed some light on why it stopped traversing the rest of the
> nodes after the foreach statement?
>
> Much thanks,
> Jared Evans
>

Can you post the code you've used to create the tree?
If it's too big, use this instruction after the creation is done:

print $root->tree_to_lol_notation({ multiline => 1 });

Then, start a new script with

use strict;
use warnings;
use Tree::DAG_Node;
#
my $lol = # cut-and-paste the output from tree_to_lol_notation here!
#
my $root = Tree::DAG_Node->lol_to_tree($lol);

After that, we should be able to reproduce your problem and find out what's
wrong.

About traversing trees, check some more docs:
http://www.perlmonks.org/index.pl?node_id=153259

Cheers

gmax


--
____ ____ _____ _ _
/ _ | (____ ( / )
( (_| | | | / ___ |) X (
___ |_|_|______(_/ _)
(_____|
MySQL Certified Professional
Sapere, saper fare, fare, far sapere
http://gmax.oltrelinux.com


Posted by Jared Evans on December 13, 2004, 10:42 am
Please log in for more thread options
Update:

The problem was solved: (much thanks to Roel van der Steen)

Here is what he said regarding my problem with my Tree::DAG_Node code
not fully traversing the tree:

-------------------------------------

It's not a bug, it's a feature. :)

>From the Tree::DAG_Node documentation:
"
This function [the callback function] must
return true or false -- if false, it will
block the next step: [...]
"

Your problem is that the foreach loop is the last statement executed in
the callback function, and the foreach returns undef, which evaluates
to false, so the walk_down procedure stops traversing. Adding a
"return 1" corrects this problem. In fact, when using callback, make
sure you always explicitly set its return value with a "return"
statement.



Similar ThreadsPosted
Class::Tree V 1.24 July 29, 2004, 7:15 am
[RFC] XML::Simple::Tree November 3, 2005, 11:37 pm
the use of removeChild() in Tree::Simple June 23, 2007, 8:18 am
html interface to a data tree December 14, 2004, 7:24 am
querying modules dependency tree on CPAN November 19, 2004, 1:54 am
HTML::Element->right/->look_down lose, I need to walk the tree June 20, 2005, 9:56 am
Problem with Net::FTP July 21, 2004, 12:40 pm
DBI - DBD-DB2 Problem - Please help June 2, 2005, 11:07 pm
Net::FTP->problem with put February 28, 2006, 2:11 am
Re: Problem with DBD::DB2 on AIX. September 20, 2006, 2:10 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap