Trouble with returning data from recursive sub

S

sabio62

G'Day,

I've got some trouble with not being able to access the data being
returned from a sub; the sub in question is supposed to return a
reference to an array of Multinode::Tree::Handle objects.
The problem is evident inside the sub itself. The sub contains a
recursive sub which in turn is updating an array beloning to the main
sub ...

I'd greatly appreciate if you can take a look at it and let me know
what to do get the handles properly ...

You may access the source code from this link:

http://www.geocities.com/sabio62/perl/build_categories_tree_with_a_hash_as_values_01.pl.txt

Some background on what I'm trying to do ...

I'm trying to build a tree that may have nodes with repeated keys in
it. For a particular key, I would like to be able to graft children
to the tree at all instances of that key.

For this purpose I've got a sub called add_children_to_str_key
that takes as parameters the string key, a hash containing the
children data to be inserted into the tree for the nodes matching the
string key, a tree object, and an optional handle to the tree, which
if given, we may start the search at that particular handle, otherwise
we start from the top of the tree.

Now, this sub add_children_to_str_key calls a recursive sub named
get_handles_on_string_key which is supposed to search the tree for all
keys that match the string key and return a reference to an array of
tree handles, which is done by matching the current handle's key,
creating a new handle, assigning it to the current handle and pushing
it onto the @found_handles, which is to be returned upon completion of
the recursion ...

Now, the problem that I'm getting is that I'm unable to get the
key/value pair from elements of the array of Tree::MultiNode::Handle's
once the recursive function has completed. The handles are there, and
they match exactly what I see inside the recursive search, but the
values upon completion via get_key, get_value or get_data, just aren't
there ... essentially 'undefined'. However, inside the recursive sub
(preorder_traverse_str_key_search) when I get a match for the key an
create a new handle and assign it it to the current handle the
key/value pairs are there ...

I take it that this has _nothing_ to do with MultiNode.pm per se, and
is more likely to be how I'm coding this stuff.
Any ideas greatly appreciated.

TIA,

Cheers, Sabio
 
X

xhoster

sabio62 said:
G'Day,

I've got some trouble with not being able to access the data being
returned from a sub; the sub in question is supposed to return a
reference to an array of Multinode::Tree::Handle objects.
The problem is evident inside the sub itself.

Well, not to me it isn't. You should rip out all the stuff that you know
is not relevant to the problem, as that is just too much cruft to expect
a stranger to wade through. (And in the process of doing that, you will
probably solve the problem ourself.)
The sub contains a
recursive sub which in turn is updating an array beloning to the main
sub ...

I don't see any recursion going on in your code. add_children seems
to call get_handles but not itself; and get_handles doesn't seem to call
either itself or add_children.
I'd greatly appreciate if you can take a look at it and let me know
what to do get the handles properly ...

You may access the source code from this link:

http://www.geocities.com/sabio62/perl/build_categories_tree_with_a_hash_a
s_values_01.pl.txt

Well, I find this part pretty befuddling:

my$found_handle=new Tree::MultiNode::Handle($tree);
#bless $found_handle,"Tree::MultiNode";
$found_handle=$handle;

What is the point to make a new Tree::MultiNode::Handle only to throw it
away?

Now, the problem that I'm getting is that I'm unable to get the
key/value pair from elements of the array of Tree::MultiNode::Handle's
once the recursive function has completed. The handles are there, and
they match exactly what I see inside the recursive search,

How do you know they match exactly what you see inside the recursive
search? And what do you mean by "handles" matching exactly? That their
reference values stringify to the same thing?


Xho
 
A

Anno Siegel

sabio62 said:
G'Day,

I've got some trouble with not being able to access the data being
returned from a sub; the sub in question is supposed to return a
reference to an array of Multinode::Tree::Handle objects.
The problem is evident inside the sub itself. The sub contains a
recursive sub which in turn is updating an array beloning to the main
sub ...

I'd greatly appreciate if you can take a look at it and let me know
what to do get the handles properly ...

You may access the source code from this link:

http://www.geocities.com/sabio62/perl/build_categories_tree_with_a_hash_as_values_01.pl.txt

Ugh. That's 290 lines of Perl. The problem should be demonstrable in
20 lines of code or less if you properly isolate it. Do that, and if
the process doesn't clear up the problem for you (it often does), then
post it and we can talk.

Anno
 
S

sabio62

G'Day Eric,

I tried your recommendation

my $found_handle = new Tree::MultiNode::Handle($handle);

and commented out

# $found_handle=$handle;

That works fine.

Sorry, I should have looked at the manual properly.
Thank you * 1000 ;-)

PS: previous reply posters ... *really*? <grin>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top