解析arrayref并添加到新的

时间:2015-10-02 13:53:46

标签: perl

我有$ arrayRef,如下所示:

[
          {
            'NUM' => '1',
            'QUOTEKEY' => 'DM343'
            'TYPE' => 'DIAMD',
            'COST' => '130'
          },
          {
            'NUM' => '1',
            'QUOTEKEY' => 'DM343'
            'TYPE' => 'ABC',
            'COST' => '150'
          },
          {
            'NUM' => '2',
            'QUOTEKEY' => 'ZX120'
            'TYPE' => 'DIAMD',
            'COST' => '100'
          },
          {
            'NUM' => '2',
            'QUOTEKEY' => 'ZX120'
            'TYPE' => 'ABC',
            'COST' => '200'
          }
]

我试图把它结合起来,所以它看起来像这样:

[
          {
            'NUM' => '1',
            'QUOTEKEY' => 'DM343'
            'DIAMD' => '130',
            'ABC' => '150'
          },
          {
            'NUM' => '2',
            'QUOTEKEY' => 'ZX120'
            'DIAMD' => '100',
            'ABC' => '200'
          }
]

我不确定最佳方式或从哪里开始循环原始数组ref以获得所需的输出。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

你所拥有的是一系列哈希。要访问每个,您需要取消引用 - 例如:

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;

my $array_ref = [
    {   'NUM'      => '1',
        'QUOTEKEY' => 'DM343',
        'TYPE'     => 'DIAMD',
        'COST'     => '130',
    },
    {   'NUM'      => '1',
        'QUOTEKEY' => 'DM343',
        'TYPE'     => 'ABC',
        'COST'     => '150',
    },
    {   'NUM'      => '2',
        'QUOTEKEY' => 'ZX120',
        'TYPE'     => 'DIAMD',
        'COST'     => '100',
    },
    {   'NUM'      => '2',
        'QUOTEKEY' => 'ZX120',
        'TYPE'     => 'ABC',
        'COST'     => '200',
    }
];

print Dumper \$array_ref;

foreach my $hash_ref (@$array_ref) {
    print Dumper \$hash_ref;
    print $hash_ref ->{'TYPE'}, "\n";
}

然而,正如问题中的评论所述 - 鉴于您的数据源是一个数据库,提取数据然后进行后处理它是无效的 - 通常最好在上游进行。

看起来我们尝试做的是合并行,这样每个记录代替COSTTYPE,它们会被转换。

这样的事情:

foreach my $hash_ref (@$array_ref) {
    print Dumper \$hash_ref;
    $type_cost{$hash_ref->{'NUM'}}{$hash_ref->{'QUOTEKEY'}}{$hash_ref->{'TYPE'}} = $hash_ref -> {'COST'};

}

print Dumper \%type_cost;

my $combined; 

foreach my $id ( keys %type_cost ) {
   foreach my $quote ( keys %{$type_cost{$id}} ) {
        push ( @$combined, { NUM => $id, QUOTEKEY => $quote, %{$type_cost{$id}{$quote}}} );
   }
}

print Dumper $combined;

这给了我们:

$VAR1 = [
          {
            'DIAMD' => '130',
            'QUOTEKEY' => 'DM343',
            'NUM' => '1',
            'ABC' => '150'
          },
          {
            'DIAMD' => '100',
            'QUOTEKEY' => 'ZX120',
            'ABC' => '200',
            'NUM' => '2'
          }
        ];

正如您所看到的 - 可能在数据库中做得更好。

答案 1 :(得分:0)

试试这个:

您不需要引用以逗号开头的字符串 =>

my $a = [              
 { NUM => '1', QUOTEKEY => 'DM343', TYPE => 'DIAMD', COST => '130' },              
 { NUM => '1', QUOTEKEY => 'DM343', TYPE => 'ABC', COST => '150' },     
 { NUM => '2', QUOTEKEY => 'ZX120', TYPE => 'DIAMD', COST => '100' },      
 { NUM => '2', QUOTEKEY => 'ZX120', TYPE => 'ABC', COST => '200' },  
];

# Add the following to get the combined output

use Data::Dumper;

my $c;

for ( @$a ){
   $c->{ $_->{ NUM } }{ QUOTEKEY } = $_->{ QUOTEKEY };
   $c->{ $_->{ NUM } }{ $_->{ TYPE } } = $_->{ COST };
}

print Dumper $c;

输出

$VAR1 = {
      '2' => {
               'QUOTEKEY' => 'ZX120',
               'DIAMD' => '100',
               'ABC' => '200'
             },
      '1' => {
               'DIAMD' => '130',
               'QUOTEKEY' => 'DM343',
               'ABC' => '150'
             }
    };