Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Re: Re: group based array sort

by Limbic~Region (Chancellor)
on Feb 19, 2004 at 22:59 UTC ( #330392=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: group based array sort
in thread group based array sort

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


  • Comment on Re: Re: Re: group based array sort
    Download Code
    Re: Re: Re: Re: group based array sort
    by bageler (Hermit) on Feb 20, 2004 at 00:05 UTC
      thanks! I'll give them both a shot.

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: note [id://330392]
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (5)
    As of 2015-07-06 03:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (70 votes), past polls