in reply to Sorting by association, a tail of dependency resolving gone nowhere

If your data is indeed stored in a hash you could do something like:

#!/usr/bin/perl -w use strict; my (%sortme, $k); %sortme = ( 1 => undef, 2 => 4, 3 => 5, 4 => 3, 5 => 1, 6 => 3 ); foreach $k ( sort { return -1 if !defined $sortme{$a}; return 1 if !defined $sortme{$b}; $sortme{$a} <=> $sortme{$b}; } keys %sortme ) { print "$k -> $sortme{$k}\n"; }
substituding the print statement with whatever is needed for your larger application. Note that my example works if two keys have the same dependency. And you'll get a warning about uninitialzed value when it tries to print $sortme{1};

Be Appropriate && Follow Your Curiosity
  • Comment on Re: Sorting by association, a tail of dependency resolving gone nowhere
  • Download Code

Replies are listed 'Best First'.
Re^2: Sorting by association, a tail of dependency resolving gone nowhere
by Roy Johnson (Monsignor) on Dec 12, 2005 at 18:16 UTC
    That doesn't produce a list in dependency order, it produces a list in numeric order of dependencies. 3, upon which 6 and 4 depend, ends up being printed last.

    Caution: Contents may have been coded under pressure.

      <forehead slap />

      Be Appropriate && Follow Your Curiosity