ukndoit:

OK, I think I see where you're going. It appears that you want to compute a score for each member, where the score may depend on the score of other members. You might want to create a variation of a [no such wiki, Topological_sort], as it's fairly easy and lets you compute things in the database for speed. The essence of it is this: You'll make several passes over the table, and on each pass, you'll perform the computation for all members whose referrals are already computed. In pseudocode, it would look something like this:

# mark members with no referrals as done, an everyone else as not done +. # NOTE: I'm assuming a new column named is_done as a work column. If +you prefer # not to have such a column in your database, you can use a temporary +table instead. $DB->do("update registered_users set is_done=0") $DB->do("update registered_users set is_done=1 where MemberID not in ( +select referrer_id from registered_users"); while (1) { # Do we have any members that aren't done yet? my @tmp = $DB->selectrow_array("select count(*) from registered_use +rs where is_done=0"); last unless $tmp[0] > 0; # Perform computation for all members whose referrals are all "done +" $DB->do(qq{ UPDATE registered_users SET is_done=1, <<<your calculation goes here>>> FROM registered_users WHERE is_done=0 AND MemberID not in ( SELECT referrer_id WHERE is_done=0 ) }); }

This way, you'll only need a few passes through the database to compute all your members, and you don't have to worry about recursive algorithms. Please note that this method can fail if your database has an error in it: If you have any loops in your database (e.g., Tom refers Dick who refers Tom or a longer loop) then the members in that loop, and any of their parents won't get computed. This shouldn't happen in your database, but if the method fails, you may want to look for an error in your data similar to this.

...roboticus


In reply to Re^5: Recursive programming question by roboticus
in thread Recursive programming question by ukndoit

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.