按Perl中的值对数组ref进行排序

时间:2018-06-27 04:16:01

标签: arrays perl sorting hash

我正在提取XML并将值作为参考传递。 参考中的数据是

$VAR1 = [ 
    {   
        'tender_data' => [
            {
                'total_mwh' => '191.4600',
                'retailer_code' => 'Origin',
                'client_num' => '20180516-03',
                'response_date' => '2018-05-22',
                'nmi' => 'NGGG000692',
                'month_name' => 'May',
                'ext_date' => '2018-5',
                'client_name' => 'Alpha Hotel Canberra',
                'result' => 'lost'
            }
        ],
        'state' => 'ACT'
    },  
    {   
        'tender_data' => [
            {
                'total_mwh' => '445.6410',
                'retailer_code' => 'ERM Power',
                'client_num' => '20110317-05',
                'response_date' => '2018-05-14',
                'nmi' => 'QB00457566',
                'month_name' => 'May',
                'ext_date' => '2018-5',
                'client_name' => 'Thunderbird Park',
                'result' => 'lost'
            },
            {
                'total_mwh' => '910.2670',
                'retailer_code' => 'AGL',
                'client_num' => '20140625-02',
                'response_date' => '2018-05-24',
                'nmi' => 'QB09015582',
                'month_name' => 'May',
                'ext_date' => '2018-5',
                'client_name' => 'Geewood Pty Ltd',
                'result' => 'undecided'
            },  

            {   
                'total_mwh' => '120.4290',
                'retailer_code' => 'AGL',
                'client_num' => '20180502-02',
                'response_date' => '2018-05-18',
                'nmi' => 'QB00493554',
                'month_name' => 'May',
                'ext_date' => '2018-5',
                'client_name' => 'Beaudesert Race Club Inc',
                'result' => 'won'
            },
        ],
        'state' => 'QLD'
    },
];

现在的问题是我正在尝试按response_date对哈希进行排序,但是我无法解决它。 我尝试了这段代码

$data = (sort { join('', (split '-', $data->{$a}->{response_date})[0,1,2]) cmp join('', (split '-', $data->{$b}->{response_date})[0,1,2]) } keys %{$data->{tender_data}});

但这给了我错误-不是哈希引用

所以我尝试对数组引用进行排序

$data = sort { join('', (split '-', $a)[0,1,2]) cmp join('', (split '-', $b)[0,1,2]) } @{$data};

但是我获得了不确定的价值。 如果我使用foreach my $tender(@{$data}){}进行循环,我将在数据引用中获取值

有人可以帮助我使用 response_date 对它进行排序 我需要对此进行排序

{
    'total_mwh' => '445.6410',
    'retailer_code' => 'ERM Power',
    'client_num' => '20110317-05',
    'response_date' => '2018-05-14',
    'nmi' => 'QB00457566',
    'month_name' => 'May',
    'ext_date' => '2018-5',
    'client_name' => 'Thunderbird Park',
    'result' => 'lost'
},
{
    'total_mwh' => '120.4290',
    'retailer_code' => 'AGL',
    'client_num' => '20180502-02',
    'response_date' => '2018-05-18',
    'nmi' => 'QB00493554',
    'month_name' => 'May',
    'ext_date' => '2018-5',
    'client_name' => 'Beaudesert Race Club Inc',
    'result' => 'won'
},

{
    'total_mwh' => '910.2670',
    'retailer_code' => 'AGL',
    'client_num' => '20140625-02',
    'response_date' => '2018-05-24',
    'nmi' => 'QB09015582',
    'month_name' => 'May',
    'ext_date' => '2018-5',
    'client_name' => 'Geewood Pty Ltd',
    'result' => 'undecided'
},

1 个答案:

答案 0 :(得分:2)

给出一个散列数组:

$data = [
  {'response_date' => '2018-05-18', 'client_name' => 'Beaudesert Race Club Inc'},
  {'response_date' => '2018-05-14', 'client_name' => 'Thunderbird Park'},
  {'response_date' => '2018-05-24', 'client_name' => 'Geewood Pty Ltd'},
];

您可以通过'response_date'键对它进行排序,

sort {$$a{response_date} cmp $$b{response_date}} @$data

例如:

# perl -MData::Dumper
$data = [
  {'response_date' => '2018-05-18', 'client_name' => 'Beaudesert Race Club Inc'},
  {'response_date' => '2018-05-14', 'client_name' => 'Thunderbird Park'},
  {'response_date' => '2018-05-24', 'client_name' => 'Geewood Pty Ltd'},
];
print Dumper([sort {$$a{response_date} cmp $$b{response_date}} @$data]);
$VAR1 = [
          {
            'client_name' => 'Thunderbird Park',
            'response_date' => '2018-05-14'
          },
          {
            'response_date' => '2018-05-18',
            'client_name' => 'Beaudesert Race Club Inc'
          },
          {
            'client_name' => 'Geewood Pty Ltd',
            'response_date' => '2018-05-24'
          }
        ];