perl 将两个散列数组Map到新的数据结构

vohkndzv  于 2022-11-15  发布在  Perl
关注(0)|答案(1)|浏览(186)

我尝试将两个哈希数组转换成一个数据结构,这个新结构是hashref,其中每个键的值都是一个哈希数组。
下面是我正在转换的两个结构-它们是在两个单独的查询中使用slice参数selectall_arrayref的结果:

$mappings = [
                {
                    bill_id        => '100',
                    linked_bill_id => '1000',
                },
                {
                    bill_id        => '100',
                    linked_bill_id => '1001',
                },
                {
                    bill_id        => '200',
                    linked_bill_id => '2000',
                },
                {
                    bill_id        => '200',
                    linked_bill_id => '2001',
                },
                {
                    bill_id        => '200',
                    linked_bill_id => '2002',
                },
];

$fees = [
                {
                    bill_id     => '100',
                    payment_id  => '500',
                    version     => 1,
                    has_fee     => 0,
                },
                {
                    bill_id     => '100',
                    payment_id  => '501',
                    version     => 2,
                    has_fee     => 1,
                },
                {
                    bill_id     => '1000',
                    payment_id  => '502',
                    version     => 1,
                    has_fee     => 0,
                },
                {
                    bill_id     => '1001',
                    payment_id  => '503',
                    version     => 1,
                    has_fee     => 0,
                },
                {
                    bill_id     => '200',
                    payment_id  => '504',
                    version     => 1,
                    has_fee     => 0,
                },
                {
                    bill_id     => '2000',
                    payment_id  => '505',
                    version     => 1,
                    has_fee     => 0,
                },
                {
                    bill_id     => '2001',
                    payment_id  => '506',
                    version     => 1,
                    has_fee     => 0,
                },
                {
                    bill_id     => '2002',
                    payment_id  => '507',
                    version     => 1,
                    has_fee     => 1,
                },
];

使用这两个结构,我想创建一个新结构,其中键是$mappings中的bill_id,值是bill_id及其所有linked_bill_id的费用数组。它应该如下所示:

$VAR1 = {
            '100' => [
                        {
                            'bill_id'    => '100',
                            'payment_id' => '500',
                            'version'    => 1,
                            'has_fee'    => 0,
                        },
                        {
                            'bill_id'    => '100',
                            'payment_id' => '501',
                            'version'    => 2,
                            'has_fee'    => 1,
                        },
                        {
                            'bill_id'    => '1000',
                            'payment_id' => '502',
                            'version'    => 1,
                            'has_fee'    => 0,
                        },
                        {
                            'bill_id'    => '1001',
                            'payment_id' => '503',
                            'version'    => 1,
                            'has_fee'    => 0,
                        },
            ],
            '200' => [
                        {
                            'bill_id'    => '200',
                            'payment_id' => '504',
                            'version'    => 1,
                            'has_fee'    => 0,
                        },
                        {
                            'bill_id'    => '2000',
                            'payment_id' => '505',
                            'version'    => 1,
                            'has_fee'    => 0,
                        },
                        {
                            'bill_id'    => '2001',
                            'payment_id' => '506',
                            'version'    => 1,
                            'has_fee'    => 0,
                        },
                        {
                            'bill_id'    => '2002',
                            'payment_id' => '507',
                            'version'    => 1,
                            'has_fee'    => 1,
                        },          
            ]
};

下面是我的代码,它让我非常接近,但并不完全是我想要的。而且,我相信有一个更好的方法来转换这些数据结构比我目前正在做的。也许这可以通过同时为两个数组链接map/grep来简化?
我正在寻求帮助,以获得正确的,并建议如何做得更好。

my $bill_fees;
for my $bill (@$mappings) {
    push @{ $bill_fees->{bill_fee}{$bill->{bill_id} },
      grep { $_->{bill_id} eq $bill->{bill_id} } @$fees;

    my @linked_bills = map  { $_->{linked_bill_id} }
                       grep { $_->{bill_id} eq $bill->{bill_id} }
                       @$mappings;

    for my $linked_bill (@linked_bills) {
        push @{ $bill_fees->{bill_fee}{$bill->{bill_id}} },
          grep { $_->{bill_id} eq $linked_bill } @$fees;
    }
}

上面的方法有一点不正确。它给出了我上面列出的预期结构,但是对于每个bill_id键,我也得到了:

use Data::Dumper;
print Dumper($bill_fees->{bill_fee});

'100' => [
           $VAR1->{'100'}[0],
           $VAR1->{'100'}[1],
           $VAR1->{'100'}[2],
           $VAR1->{'100'}[3],
],
'200' => [
           $VAR1->{'200'}[0],
           $VAR1->{'200'}[1],
           $VAR1->{'200'}[2],
           $VAR1->{'200'}[3],
           $VAR1->{'200'}[0],
           $VAR1->{'200'}[1],
           $VAR1->{'200'}[2],
           $VAR1->{'200'}[3],           
]

我假设这是因为Map中有多个相同的bill_id,所以,例如,bill_id = 100将被循环两次。

wqsoz72f

wqsoz72f1#

首先,从费用的账单ID中提取一个Map到bill_fees的键,然后,将费用分隔到相应的账单ID下:

my %idmap = map {
    $_->{bill_id}        => $_->{bill_id},
    $_->{linked_bill_id} => $_->{bill_id}
} @$mappings;
my $bill_fees;
for my $fee (@$fees) {
    push @{ $bill_fees->{ $idmap{ $fee->{bill_id} } } }, $fee;
}

请注意每个账单标识如何链接到自身,以简化检索。

相关问题