in reply to Recursion question...

You need to my() the @sorted_children array! Otherwise, you overwrite it!

Better yet, don't use the array, and use the default sort().

if (@children) { print "<li>[$rec{'ID'}] $rec{'Subject'}<ul>\n"; for my $child (sort @children) { do_tree($child); } print "</ul></li>\n"; }
Notice how I left out the {$a cmp $b} part -- that's how sorting behaves by default.

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Re: Recursion question...
by c0d34w4y (Acolyte) on Dec 15, 2001 at 13:13 UTC
    Anonymous Monk, that's a nice code you wrote ;-).
    
    As I looked at it, though, I couldn't help but 
    hack it a bit here and there.  My hope was to shorten
    it up a bit (which, who knows, may lead to some
    performance improvements? ;).  So, if you like the
    change just stick to it... I'd be glad to know that
    it was useful to you. 
    
    Anyhow, here we go:
    
    sub do_tree { # -------------------------------------------------------- # this is the tree loop that call itself to display records in # tree form. pass it some record ids and see what happens! my %rec = &get_record ($_[0]); # get all the kids; weed out empty ones my @children = sort grep( {length} , split (/ /, $rec{'Children'})); + # no kids? actually display the record then print "<li>[$rec{'ID'}] $rec{'Subject'}</li>\n" unless ($#children); # kinds actually exist? display them... print "<li>[$rec{'ID'}] $rec{'Subject'}<ul>\n"; do_tree($child) for my $child (@children); print "</ul></li>\n"; }
    ps. i didn't test it or anything... however, i feel like it should work without much hassle on your part.


    --
    print join(" ", map { sprintf "%#02x", $_ }unpack("C*",pack("L",0x1234 +5678)))
      Specifically, you didn't test this line: do_tree($child) for my $child (@children); A post-fix for must operate on $_ -- you're not allowed to give an iterator variable. do_tree($_) for @children;

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        Oh yeah.... definitely! ;-).
        
        THankx for the hint.
        
        ps.
        I keep telling myself to quit doing any hacking at
        2:00 a.m. in the morning.  
        


        --
        print join(" ", map { sprintf "%#02x", $_ }unpack("C*",pack("L",0x1234 +5678)))