As still nobody has a really good solution for that hierarchy sort problem I had to hack the following:
#!/usr/bin/perl
use strict;
use Data::Dumper;
my $form = "%30s %s\n";
my %upgrades;
my %lvl;
my %PofC = (
1 => 2
, 3 => 2
, 4 => 2
, 2 => 5
, 6 => 5
, 7 => 4
, 5 => undef
, 8 => 4
);
print Dumper \%PofC;
foreach my $child (keys %PofC) {
$lvl{$child} = 0;
}
my $leftovers = 1;
my $level = 0;
while ($leftovers > 0) {
%upgrades = ();
$leftovers = 0;
foreach my $child (keys %PofC) {
if (exists $lvl{$PofC{$child}}
&& $lvl{$PofC{$child}} == $level)
{
$upgrades{$child} = 1;
++$leftovers;
}
}
foreach (keys %upgrades) {
++$lvl{$_};
}
++$level;
}
print Dumper \%lvl;
printf $form, 'child', 'parent';
foreach my $c ( sort { $lvl{$a} <=> $lvl{$b} } keys %lvl ) {
printf $form, $c, $PofC{$c};
}
exit;
__OUTPUT__
# %PofC
$VAR1 = {
1 => 2,
2 => 5,
3 => 2,
4 => 2,
5 => undef,
6 => 5,
7 => 4,
8 => 4
};
# %lvl
$VAR1 = {
1 => 2,
2 => 1,
3 => 2,
4 => 2,
5 => '0',
6 => 1,
7 => 3,
8 => 3
};
child parent
5
2 5
6 5
4 2
3 2
1 2
7 4
8 4
|