in reply to Using Data::Dumpers Sortkeys subroutine

I solved it with this less-than-ideal hack which works for this special case:
84 sub hash_sort { 85 my ($hash) = @_; 86 if (ref %$hash != 'REF') { 87 return [ 88 (sort {$a <=> $b} keys %$hash) 89 ]; 90 } else { 91 return [ 92 (sort {$a cmp $b} keys %$hash) 93 ]; 94 } 95 }
It tests to see if the value of the has is a reference to the anonymous hash. If not, it sorts it numerically, otherwise it gets sorted alphabetically. What would be a more robust way of solving this?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon";
$nysus = $PM . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re^2: Using Data::Dumpers Sortkeys subroutine
by almut (Canon) on Nov 27, 2008 at 08:01 UTC
    What would be a more robust way of solving this?

    I think natural sort - as Corion suggested - would fit the bill quite well:

    use Data::Dumper; use Sort::Naturally; $Data::Dumper::Sortkeys = sub { my ($hash) = @_; return [ nsort( keys %$hash ) ]; };
      Hmm, that results in an error:
      Bizarre copy of ARRAY in return at /usr/lib/perl/5.8/Data/Dumper.pm li +ne 511, <FH> line 130. Segmentation fault

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks