in reply to Re^2: Recursive programming question
in thread Recursive programming question

Try this: (Monks, please tell me if I am wrong assuming this would work) UnTested...
use vars qw/$reg_u_tbl $dbh $sth $rows $_autoField $_referField /; $reg_u_tbl = "registered_users"; $_referField = "referrer_id"; $_autoField = "MemberId"; $_rankField = "ranked1"; $sth = $dbh->prepare("select * from `$reg_u_tbl` where ##RequirementsH +ere##"); $sth->execute; while($rows = $sth->fetchrow_hashref()) { my $thisMembersid = $rows->{$_autoField}; my $referrersid = $rows->{$_referField}; my $rankName = $rows->{$_rankField}; my $startinglevel = 0; &buildTradRank ($thisMembersid,$referrersid,$rankName,$startinglevel +); } $sth->finish(); sub buildTradRank { ($_rankedid,$_startingid,$_rank,$_level) = @_; my $_fieldName = $_rank . '_traditional'; $_level++; $_countLevels++; $_highLevel = $_level if $_level > $_highLevel; if($_level == 1) { my $_addToSearch = ""; } else { my $_addToSearch = qq~ and `$_rank` = "0"~; } $sth2 = $dbh->prepare(qq{select * from `$reg_u_tbl` where `$_refer +Field` = ?$_addToSearch}); $sth2->execute($_startingid); while(my $_nregu = $sth2->fetchrow_hashref()) { if(!$_nregu->{$_fieldName} || ($_nregu->{$_fieldName} && $_nre +gu->{$_fieldName} != $_rankedid)) { my $_updated = $dbh->do(qq{update `$reg_u_tbl` set `$_fiel +dName` = ? where `$_autoField` = ?}, undef, $_rankedid, $_nregu->{$_a +utoField}); if($_updated) { $_countAllNewlyAdded++; } else { $_countAllNewlyAddedFailures++; $_dbi_errors .= $DBI::errstr . "\n" if $DBI::errstr; } } if(!$_nregu->{$_rank}) { my $_countReferred = $dbh->selectrow_array(qq{select count +(*) from `$reg_u_tbl` where `$_referField` = ? and `$_rank` = "0"}, u +ndef, $_nregu->{MemberId}); if(!$_countReferred) { next; } # Send back to this routine when it returns it should cont +inue with the current data, starting it over SHOULD fork a new routin +e... &buildTradRank($_rankedid,$_nregu->{$_autoField},$_rank,$_ +level); } next; } return 1;#Return when finished... }
By creating one subroutine, you should be able to call it in the middle of it, which should start a new process, with new data and when it returns, then it should continue where it left off... You had it just about right, I only seen an error in your subroutine, you did not add the call to catch the data you passed to the subroutine...

Monks, am I correct in this?

Lee