Question

I have a hash of hashes like this:

%hash = {
    car => {
        '10' => 'y',
        '11' => 'y',
        '12' => 'y',
        '13' => 'y'
    },
    bus => {
        '10' => 'y',
        '11' => 'y',
        '12' => 'y',
        '13' => 'y'
    },
    tr => {
        '10' => 'y',
        '11' => 'y',
        '12' => 'y',
        '13' => 'y'
    }
}

How can I can in PERL to print this hash of hashes (using Dumper) where the keys of each section are sorted (10,11,12,13,14...) in the above order?

Thanks in advance,

Miki

Was it helpful?

Solution

You can try

use Data::Dumper;
local $Data::Dumper::Sortkeys = 1;
# using custom sort function
# local $Data::Dumper::Sortkeys = sub { [ sort keys %{+shift} ] };

print Dumper {
    car => {
        '10' => 'y',
        '11' => 'y',
        '12' => 'y',
        '13' => 'y'
    },
    bus => {
        '10' => 'y',
        '11' => 'y',
        '12' => 'y',
        '13' => 'y'
    },
    tr => {
        '10' => 'y',
        '11' => 'y',
        '12' => 'y',
        '13' => 'y'
    }
}

output

$VAR1 = {
      'bus' => {
                 '10' => 'y',
                 '11' => 'y',
                 '12' => 'y',
                 '13' => 'y'
               },
      'car' => {
                 '10' => 'y',
                 '11' => 'y',
                 '12' => 'y',
                 '13' => 'y'
               },
      'tr' => {
                '10' => 'y',
                '11' => 'y',
                '12' => 'y',
                '13' => 'y'
              }
    };

From perldoc Data::Dumper

$Data::Dumper::Sortkeys

Can be set to a boolean value to control whether hash keys are dumped in sorted order. A true value will cause the keys of all hashes to be dumped in Perl's default sort order. Can also be set to a subroutine reference which will be called for each hash that is dumped. In this case Data::Dumper will call the subroutine once for each hash, passing it the reference of the hash. The purpose of the subroutine is to return a reference to an array of the keys that will be dumped, in the order that they should be dumped. Using this feature, you can control both the order of the keys, and which keys are actually used. In other words, this subroutine acts as a filter by which you can exclude certain keys from being dumped. Default is 0, which means that hash keys are not sorted.

OTHER TIPS

See Sortkeys in Data::Dumper. You can set it to a subroutine that sorts the hash keys.

If you want keys of a hash to be outputed in order, you need to sort it before you print them.

If you want to use Data::Dumper to dump your hash and want its keys are dumped in sorted order, you can set $Data::Dumper::Sortkeys to true, then dump it normally.

You can sort keys of a hash manually like this:

#!/usr/bin/perl

use strict;
use warnings;

use feature qw(switch say);

my $hash = {
    car => {
        '10' => 'y',
        '11' => 'y',
        '12' => 'y',
        '13' => 'y'
    },
    bus => {
        '10' => 'y',
        '11' => 'y',
        '12' => 'y',
        '13' => 'y'
    },
    tr => {
        '10' => 'y',
        '11' => 'y',
        '12' => 'y',
        '13' => 'y'
    }
};

foreach my $key0 (sort keys %$hash) {
    say "$key0";
    foreach my $key1 (sort { $a <=> $b } keys %{$hash->{$key0}}) {
        say "\t$key1 $hash->{$key0}->{$key1}";
    }
}

Testing:

$ perl t.pl 
bus
    10 y
    11 y
    12 y
    13 y
car
    10 y
    11 y
    12 y
    13 y
tr
    10 y
    11 y
    12 y
    13 y
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top