Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
bageler,
While I really think using Sort::Tree is probably the best option, here is a purely iterative solution. It has the following requirements:
  • IDs will always be numerical
  • Root level nodes will always have a parent of 0
  • Probably a few others
    #!/usr/bin/perl use strict; use warnings; my @list = ( {id => 6, parent => 2, level => 3, name => '1234' }, {id => 2, parent => 1, level => 2, name => 'bar2' }, {id => 4, parent => 1, level => 2, name => 'asdf' }, {id => 3, parent => 1, level => 2, name => 'blah' }, {id => 1, parent => 0, level => 1, name => 'foo1' }, {id => 10, parent => 0, level => 1, name => '****' }, {id => 9, parent => 10, level => 2, name => 'qwer' }, {id => 5, parent => 2, level => 3, name => 'lev3' }, {id => 7, parent => 5, level => 4, name => 'wxyz' }, ); my $tree = Build_Tree( \@list ); Print_Tree( $tree ); sub Build_Tree { my $list = shift; my %tree; for ( @$list ) { $tree{$_->{id}}{level} = $_->{level}; $tree{$_->{id}}{name} = $_->{name}; push @{ $tree{$_->{parent}}{children}} , $_->{id}; } return \%tree; } sub Print_Tree { my $tree = shift; my %seen; my @stack = sort { $b <=> $a } @{ $tree->{0}{children} }; while ( @stack ) { my $id = pop @stack; if ( $seen{$id} ) { print ' ' x (($tree->{$id}{level} - 1) * 5), "Circular det +ection\n"; next; } $seen{$id}++; print ' ' x (($tree->{$id}{level} - 1) * 5), $tree->{$id}{name +}, "\n"; push @stack , sort {$b <=> $a} @{$tree->{$id}{children}} if ex +ists $tree->{$id}{children}; } }
    Cheers - L~R

    In reply to Re: Re: Re: group based array sort by Limbic~Region
    in thread group based array sort by bageler

    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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (7)
    As of 2014-07-29 21:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (228 votes), past polls