in reply to Sorting by association, a tail of dependency resolving gone nowhere
If you'd rather roll your own, I thought this solution was rather elegant:
my %dmap = ( 1 => undef, 2 => 4, 3 => 5, 4 => 3, 5 => 1, ); my @sorted; my %sorted_contents; my %tried; sub insert { my $candidate = $_[0]; return if exists $sorted_contents{$candidate}; die "Cycle!\n" if $tried{$candidate}++; my $dependency = $dmap{$candidate}; insert($dependency) if (defined $dependency); $sorted_contents{$candidate} = push @sorted, $candidate; } insert $_ for keys %dmap; print "@sorted\n";
|
|---|