At 10:54 20.06.2001 +0200, Dalløkken, Espen wrote:
>I'm trying to wirte a recursive subroutine that recives an array, does some
>processing and then calls itself with a new array.
>The problem is that I never get to retrive the array after the subroutine
>has ran once. When it runs the second time it doesn't get the correct set of
>items in the passed in array.
>So I'm wondering if I'm calling the subroutine in the wrong way, or if I'm
>retriving the parameters incorrectly.
>
>---
>&buildBranch(@someNodes);
You don't have to use & anymore. I'd call your routines like this:
buildBranch(@someNodes);
also, you probably want to get into the habit of passing references to
structures instead of the structure itself. That means you don't have to
return the structure if you change it inside your function, and it's faster
because you're not making copies of structures all over the place.
That said:
buildBranch(\@someNodes); # \@ passes a reference to the array, not a copy
of the array. changes made inside buildBranch() will effect @someNodes.
sub buildBranch($) # I know that prototyping is controversial, so maybe
we'll get a good thread going out of this sub declaration
{
my($raNodes) = @_; # if you're not using strict, the gods of Perl
will grumble -- and besides, it's better.
foreach my $node (@{ $raNodes }) # I'm changing your variable name
to something a little more descriptive
{
if($node->getTagName() eq "branch") # could this just be a
property of node? like $node->{TagName} ? just an idea
{
# fetch parameters from the XML file
... # this looks cool
CreateBranch();
# no reason to right the same code twice, so we'll
move the child nodes stuff outside the if
}
my $raChildNodes = $node->GetChildNodes(); # you'll have
to change GetChildNodes() to return an array ref instead of an array
buildBranch($raChildNodes) if(scalar(@{ $raChildNodes }) >
0); # as long as GetChildNodes() returns a proper array of child nodes, it
should work as is. what specific errors are you getting?
}
}
>sub buildBranch()
>{
> @numNodes = @_;
> foreach $item (@numNodes)
> {
> # if the node is a branch tag, create branch
> if ($item->getTagName() eq "branch")
> {
> # fetch parameters from the XML file
> $branchName = $item->getAttribute("name");
> $vpath = $item->getAttribute("vpath");
> $comment = $item->getAttribute("comment");
> $edition = $item->getAttribute("edition");
> $owner = $item->getAttribute("owner");
> $groupForSharing =
>$item->getAttribute("groupForSharing");
>
> &createBranch();
> @childBRNodes = $item->getChildNodes();
> if (scalar(@childBRNodes) > 0)
> {
> &buildBranch(@childBRNodes);
> }
> }
> @childNodes = $item->getChildNodes();
> if (scalar(@childNodes) > 0)
> {
> &buildBranch(@childNodes);
> }
> }
>}
Aaron Craig
Programming
iSoftitler.com