moderated >> a problem using Tree::DAG_Node?

by jnevans » Fri, 10 Dec 2004 05:19:54 GMT


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" , @{$root->draw_ascii_tree};
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]->{_depth};
if ( $_[0]->{_depth} eq 0 ) {
print "\n" ;
}
elsif ( $_[0]->{_depth} eq 1 ) {
if ( $found_customer eq 1 ) {
print "write footer email\n";
print " " x $_[0]->{_depth};
}
print "header email - Customer\# " . $node->name . " "
. $node-> attributes->{'name'}. " " . $node->attributes->{'email'} .
"\n" ;
$found_customer = 1;
$f0 = "";
$f1 = "";
$f2 = "";
$f3 = "";
$f4 = "";
@customized_letter_lines = "";
$f0 = $node->attributes->{'email'};
$f1 = $node->attributes->{'name'};
$f2 = $node->name;

my @letter_lines = @header_lines;

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

},
_depth => 0
});

if ( $found_customer eq 1 ) {
print " write footer email\n";
}

output is:

header email - Customer# 2000886 Happy, Inc. XXXX@XXXXX.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. XXXX@XXXXX.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


moderated >> a problem using Tree::DAG_Node?

by Jared Evans » Tue, 14 Dec 2004 01:44:49 GMT


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. :)


"
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 Threads

1. a problem using Tree::DAG_Node?

2. using tree and entries with spaces

3. Tree view using tkx module.

4. Tree view of directory while using Subversion

5. Problem with Tk::Tree and indicator

6. Walking a tree and extracting info... Problems

I am new to the perl thing and i am trying to extract some date from
some web pages and am having problems....  can someone please tell me
what i am doing wrong... i think i have become a charter member of the
"idiots 'r' us" club... :o)!

this is my script... pretty simple so far, i am just trying to get one
piece of info working to start.  i can traverse the directory and print
the filenames, but it only seems to get the data and do the pattern
matching from the first file in the directory....

any hints would be appreciated!

#!/usr/bin/perl
$dir="/Users/test/";
opendir(DIRECTORY, $dir) || die("Cannot open directory");
@thefiles= readdir(DIRECTORY);
closedir(DIRECTORY);

foreach $file (@thefiles) {
  unless ( ($file eq ".") || ($file eq "..") || ($file eq ".DS_Store")
) {
    open FILE, "$dir/$file" or die "Can't open $file : $!";
    while( <FILE> ) {
      s/\t//;            # ignore tabs by erasing them
      next if /^(\s)*$/;  # skip blank lines
      chomp;              # remove trailing newline characters
      push @lines, $_;    # push the data line onto the array
    }
    close FILE;
    $string = "@lines";
    $n++;
    print "$n:$file:";
    $string =~ /<span class=searchtitle><B> (.*?)<\/B><\/span><BR>/is;
    print "$1\n";    # print html page title
  }
}

7. After authentication Tree view is not working properly. - ASP.NET Web Controls

8. Tree View Double Click Event Handler not working properly

Hello,
I have a tree view which has Check Box's beside each node.
In the BeforeCheck event Handler I have canceled the event to
prevent the user from Checking/ Un-checking the box.
I am trying to now set up the DoubleClick event but the first time
the user double clicks the event is not called. For example the first
time clicked it will expand/Collapse the node but the event will not be 
called. Every time after that it works correctly.
A co-worker also has a similar problem with right clicking to bring up
a context menu. The first time it does not work then every time after 
that it works as expected.
Any ideas would is appreciated.
-Navid-