Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://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":



    • Are you posting in the right place? Check out Where do I post X? to know for sure.
    • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
      <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
    • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
    • Want more info? How to link or How to display code and escape characters are good places to start.
  • Log In?
    Username:
    Password:

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

    How do I use this?Last hourOther CB clients
    Other Users?
    Others studying the Monastery: (5)
    As of 2024-04-19 15:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found