Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

sortkeys for Data::Dump

by Anonymous Monk
on Jul 21, 2013 at 23:58 UTC ( #1045545=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

anyone seen a sortkeys (like Data::Dumper->new->Sortkeys() ) for Data::Dump?

Replies are listed 'Best First'.
Re: sortkeys for Data::Dump
by rjt (Deacon) on Jul 22, 2013 at 00:04 UTC

    There is no analogous method, because Data::Dump sorts the keys by default. (Confirmed by inspection of Data/

    #!/usr/bin/env perl use 5.012; use warnings; use Data::Dump qw/dump/; my $data = { key1 => 'foo', key2 => 3, key0 => 'bar', }; dump $data;


    { key0 => "bar", key1 => "foo", key2 => 3 }

    If you need to customize the sort subroutine, however, you will need to hook into Data::Dump::Filtered. You did not specify which usage you need, but you supplied empty arguments to Sortkeys() in your OP, so I'll assume the former for now.


      Thanks, I know :)

      ... hook ...

      I don't think so, I think a patch is in order, but I give up

        ... hook ...

        I don't think so

        Have a little faith. :-)

        Easily extendable to support arbitrary sort subs, or even context-sensitive sorts.

        my $dump = dumpf($data, sub { use List::Util qw/max/; my ($ctx, $obj) = @_; my %r; state %seen; if ('HASH' eq ref $obj and not $seen{$obj}++) { no warnings 'uninitialized'; # Unknown sort keys my $len = max map { length } keys $obj; # Keep results aligned my $sort_string = 'a' x max map { length } keys $obj; # Convert existing keys to ...aaaaa, ...aaaab, ...aaaac, etc., # so Data::Dump's lexical sort works. my %keymap = map { $sort_string++ => $_ } sort { $num{$a} <=> $num{$b} } keys $obj; $obj->{$_} = delete $obj->{$keymap{$_}} for keys %keymap; my $dump = Data::Dump::dump($obj); # Replace to get original keys back $dump =~ s/$_/sprintf "%-${len}s",$keymap{$_}/e for keys %keym +ap; $r{dump} = $dump; } return \%r; });

        Full example


        { unknown => "unknown key", one => "first", two => { one => "the", six => "inner", seven => "hash", eight => "works", nine => "too", }, three => "third", }

        I think a patch is in order,

        Not a bad idea, either.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1045545]
Approved by ww
Discipulus without Corion and with choroba in another timezone the monastery was silent.. I was silbing alone in the cloister
[choroba]: Yes, I feel alone in the cyberspace...
[choroba]: Not many occasions to talk to my wife, either :-(
Discipulus s/silbing/whistle/ dunno from where this word was in my mind..
[Discipulus]: lost in time in space and in the meaning (rocky horror picture show)
[Corion]: Discipulus: Yeah, a short vacation on the Canary islands (Lanzarote). Always great to be in the warmth and sun, from a cold and rainy February here ;)

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2017-02-22 08:44 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (325 votes). Check out past polls.