在Perl中以简单哈希转换多级哈希

时间:2015-06-04 05:15:06

标签: perl hash

我有一个多级哈希的哈希,我希望将其转换为单一级别。但是我无法做到这一点

实际哈希:

'MainSlab' => {
                'A1' => {
                           'Slab' => {
                                     '49_168' => {
                                                   'Amt' => '3000',
                                                   'Start' => '49',
                                                   'End' => '168'
                                                 },
                                     '169_720' => {
                                                    'Amt' => '3000',
                                                    'Start' => '169',
                                                    'End' => '720'
                                                  },
                                     '2_48' => {
                                                 'Amt' => '3000',
                                                 'Start' => '2',
                                                 'End' => '48'
                                               },
                                     '721_-' => {
                                                  'Amt' => '3000',
                                                  'Start' => '721',
                                                  'End' => '-'
                                                }
                                }
                         },
                     'A2' => {
                       'Slab' => {
                                 '49_168' => {
                                               'Amt' => '3000',
                                               'Start' => '49',
                                               'End' => '168'
                                             },
                                 '169_720' => {
                                                'Amt' => '4000',
                                                'Start' => '169',
                                                'End' => '720'
                                              },
                                 '2_48' => {
                                             'Amt' => '5000',
                                             'Start' => '2',
                                             'End' => '48'
                                           },
                                 '721_-' => {
                                              'Amt' => '3000',
                                              'Start' => '721',
                                              'End' => '-'
                                            }
                            }
                     }
              }

我希望将其转换为简单的单级哈希,如下所示:

slab =>{
                "49_168"=>{"A1"=> "3000","A2"=>"3000"},
                "169_720"=>{"A1"=>"4000","A2"=>"4000"},
                "2_48"=>{"A1"=>"5000","A2"=>"5000"},
                "721_"=>{"A1"=>"3000","A2"=>"3000"}
        }

请帮我这样做我们怎么做呢

2 个答案:

答案 0 :(得分:1)

假设

my %hash = (
    'MainSlab' => {
                'A1' => {
                           'Slab' => { 
                                      '49_168' => {
                                               'Amt' => '3000',
                                               'Start' => '49',
                                               'End' => '168'
                                             },
                 'A2' => ...
);  

然后:

my $hashref     = $hash{'MainSlab'};
my $new_hashref = {};
foreach my $ax (keys %$hashref) {
   foreach my $k (keys %{$hashref->{$ax}{'Slab'}}) {
     $new_hashref->{$k}{$ax} = $hashref->{$ax}{'Slab'}{$k}{'Amt'};
   }
}
my %new_hash    = (slab => $new_hashref);

将产生:

$new_hash = ( 'slab' => {
                 '49_168' => {
                        'A1' => '3000',
                        'A2' => '3000'
                      },
                 '169_720' => {
                         'A1' => '3000',
                         'A2' => '4000'
                 ...
           );

答案 1 :(得分:0)

use Data::Dumper qw();

## actual hash $h1
my $h1 = { 'MainSlab' => { 'A1' => { 'Slab' => { '49_168' => { 'Amt' => '3000', 'Start' => '49', 'End' => '168' }, '169_720' => { 'Amt' => '3000', 'Start' => '169', 'End' => '720' }, '2_48' => { 'Amt' => '3000', 'Start' => '2', 'End' => '48' }, '721_-' => { 'Amt' => '3000', 'Start' => '721', 'End' => '-' } } }, 'A2' => { 'Slab' => { '49_168' => { 'Amt' => '3000', 'Start' => '49', 'End' => '168' }, '169_720' => { 'Amt' => '4000', 'Start' => '169', 'End' => '720' }, '2_48' => { 'Amt' => '5000', 'Start' => '2', 'End' => '48' }, '721_-' => { 'Amt' => '3000', 'Start' => '721', 'End' => '-' } } } } };

## transform to $h2
my @l2 = keys(%{$h1->{'MainSlab'}});
my @l1 = keys(%{$h1->{'MainSlab'}->{$l2[0]}->{'Slab'}});
my $h2 = {};
foreach my $l1 (@l1) {
    my $inner = {};
    foreach my $l2 (@l2) {
        $inner->{$l2} = $h1->{'MainSlab'}->{$l2}->{'Slab'}->{$l1}->{'Amt'};
    } ## end foreach
    $h2->{'slab'}->{$l1} = $inner;
} ## end foreach

## print result
print(Data::Dumper->Dump([$h2],['$h2']));

输出:

$h2 = {
        'slab' => {
                    '49_168' => {
                                  'A1' => '3000',
                                  'A2' => '3000'
                                },
                    '169_720' => {
                                   'A1' => '3000',
                                   'A2' => '4000'
                                 },
                    '2_48' => {
                                'A1' => '3000',
                                'A2' => '5000'
                              },
                    '721_-' => {
                                 'A1' => '3000',
                                 'A2' => '3000'
                               }
                  }
      };