Next Trivial Utilities 69

sortby

        for my $m (keys %new_number) {
          delete $new_number{$m} if $new_number{$m} == $m;
        }

        while (%new_number) {
          my ($cur) = keys %new_number;
          my @chain;
          do {
            push @chain, $cur;
            $cur = delete $new_number{$cur};
          } while $cur != $chain[0];
          print STDERR "Chain: (@chain)\n" if $VERBOSE;

          my ($prev, @rest) = reverse @chain;
          my $TMP = "$prev.TMP";
          rename("$dir/$prev", "$dir/$TMP") or die "$prev => $TMP: $!";
          for my $cur (@rest) {
            rename("$dir/$cur", "$dir/$prev") or die "$cur => $prev: $!";
            $prev = $cur;
          }
          rename("$dir/$TMP", "$dir/$prev")  or die "$TMP => $prev: $!";
        }



Next Menu Copyright © 2012 M. J. Dominus