0

我有一个如下所示格式的对象,我需要按可用月份对它们进行分组。1000、2000 等。是以美元为单位的金额,下一级是 (6, 4) 是月份。

我的对象:

$VAR1 = {
          '1000' => {
                      '6' => {
                               'APR' => '13.9'
                             },
                      '4' => {
                               'APR' => '11.9'
                             }
                    },
          '2000' => {
                      '6' => {
                               'APR' => '13.9'
                             },
                      '4' => {
                               'APR' => '11.9'
                             }
                    },
          '4000' => {
                      '6' => {
                               'APR' => '13.9'
                             },
                      '4' => {
                               'APR' => '11.9'
                             }
                    }
          '5000' => {
                      '6' => {
                               'APR' => '13.9'
                             }
                    }
        };

我想要的对象:

$VAR1 = {
    '6' => {
             '1000',
             '2000',
             '4000',
             '5000'
           },
    '4' => {
             '1000',
             '2000',
             '4000'
           }
}
4

1 回答 1

2
my %new_hash;
for my $amount (sort { $a <=> $b } keys(%$VAR1)) {
   for my $month (keys(%{ $VAR1->{$amount} })) {
      push @{ $new_hash{$month} }, $amount;
   }
}
于 2013-07-25T18:13:59.860 回答