[ic] recursive perl subroutine

interchange-users@icdevgroup.org interchange-users@icdevgroup.org
Tue Sep 3 13:21:01 2002


On Tue, Sep 03, 2002 at 06:27:15PM +0100, John Allman wrote:
> hi - i'm trying to trace through a reasonably shallow category tree to 
> find if a product fits into a particular category. i want a function to 
> return 0 if the products category, or it's category's category etc is 
> equal to the WCODE variable defined in catalog.cfg and 0 otherwise (0 
> being the root node of the tree).
> 
> the function below is an effort to do that. if i try calling 
> checktree(0) it returns 1 as requested and if i try calling 
> checktree(200) it returns 0 (the value of WCODE is 200). so far so good.
> 
> if i try checktree(201) it doesn't seem to return anything. the entry in 
> cattable with code 201 has pcode 200. checktree should recursively pass 
> the value of pcode until it hits 200 or 0 (it is guaranteed to hit one 
> or the other) i have gotten it to return $pcode instead of 
> checktree($pcode) and it returns 200 which is correct. but it doesn't 
> seem to like passing $pcode back into itself.
> 
> error.log contains no errors. checktree is defined inside a [perl] block 
>  in a page.


If this is a guaranteed shallow tree, you might write it instead as an
SQL LEFT JOIN.  If you know it is only three levels, that will
be just two or three depending on your root.

> 
> the tree looks something like this
> 			0
> 			|
> 		-----------------
> 		|		|
> 		200		other numbers
> 		|
> 	-----------------
> 	|		|
> 	201		other numbers
> 
> code is the identifier for the node and pcode is the identifier for its 
> parent node.
> 
> sub checktree {
>                         my ($catcode)=@_;
>                         if($catcode == 0)
>                         {
>                                 return 1;
>                         }
>                         elsif($catcode == $Variable->{WCODE})
>                         {
>                                 return 0;
>                         }
>                         else
>                         {
>                                 $db = $Db{cattable};
>                                 $sql = "select pcode from cattable 
> where pcode=".$catcode;
>                                 $parent = $db ->query({sql => "select 
> pcode from cattable where code=".$catcode});
>                                 $row = shift (@$parent);
>                                 ($pcode) = @$row;
>                                 return checktree($pcode);
>                         }
> }
> 
> is there something built into interchange to stop recursive calls? or is 
> there a syntax error in my code (i in no way claim to be a perl expert)?
> 
> thanks in advance!
> 
> John
> 
> _______________________________________________
> interchange-users mailing list
> interchange-users@icdevgroup.org
> http://www.icdevgroup.org/mailman/listinfo/interchange-users
> 

-- 

Christopher F. Miller, Publisher                               cfm@maine.com
MaineStreet Communications, Inc           208 Portland Road, Gray, ME  04039
1.207.657.5078                                         http://www.maine.com/
Content/site management, online commerce, internet integration, Debian linux