合并哈希数组中的哈希属性



我正在解析一个 excel 电子表格,并尝试跨表和文件按 id 合并数据行。 这是我所拥有的精简版。 在 PHP/JS 背景下,我更喜欢将哈希概念化为对象,所以 %aoo 表示对象数组而不是哈希数组......

#!/usr/bin/env perl
use v5.10.0;
use strict;
use warnings;
use Data::Dump;
use Data::Dumper;
# Array of objects
# Each object is a row from a table
my $aoo1 = [
    {
        "id"    => 1,
        "name"  => "Dan",
        "team"  => "red"
    },
    {
        "id"    => 2,
        "name"  => "Arnold",
        "team"  => "red"
    },
    {
        "id"    => 3,
        "name"  => "Kristen",
        "team"  => "red"
    }
];
my @aoo2 = (
    {
        "id"    => 1,
        "position"  => "web developer",
    },
    {
        "id"    => 2,
        "position"  => "CEO",
    },
    {
        "id"    => 3,
        "position"  => "Secretary",
    }
);
my @aoo3 = (
    {
        "id"    => 1,
        "tenure"  => "1yr",
    },
    {
        "id"    => 2,
        "tenure"  => "25yr",
    },
    {
        "id"    => 3,
        "tenure"  => "5yr",
    }
);
# object of arrays
# each property is a table name from spreadsheet
my %ooa;
%ooa = (
    "People List" => $aoo1,
    "Position List" => @aoo2,
    "Tenure List" => @aoo3
);
# dd %ooa;
while (my ($list_name, $aoo) = each %ooa)
{
    # $aoo reftype is array | [ %object, %object, %object ]
    # Do something to look into other objects for same id...
}

我希望能够为文件中的每个唯一行创建一个新对象,以便我可以过滤值,然后将其写入 CSV 文件。

例如最终结果

%complete_row = (
    'id' => 1,
    'name' => 'Dan',
    'team' => 'red',
    'position => 'Web Dev',
    'tenure' => '1yr'
);

将第二个和第三个数组放入将 ID 映射到哈希的哈希中。 然后遍历人员并使用 ID 从职位和任期哈希中获取数据。

use strict;
use warnings;
use Data::Dumper;
my $people = [
    {
        id    => 1,
        name  => "Dan",
        team  => "red"
    },
    {
        id    => 2,
        name  => "Arnold",
        team  => "red"
    },
    {
        id    => 3,
        name  => "Kristen",
        team  => "red"
    }
];
my $positions = [
    {
        id => 1,
        position => "web developer",
    },
    {
        id => 2,
        position => "CEO",
    },
    {
        id => 3,
        position => "Secretary",
    }
];
my $tenures = [
    {
        id => 1,
        tenure => "1yr",
    },
    {
        id => 2,
        tenure => "25yr",
    },
    {
        id => 3,
        tenure => "5yr",
    }
];
# hash each by ID
my %position_hash = map { $_->{id} => $_ } @$positions;
my %tenure_hash   = map { $_->{id} => $_ } @$tenures;
# combine
my $complete = [];
foreach my $person (@$people) {
    my $id = $person->{id};
    my $complete_row = {
        %$person,
        position => $position_hash{$id}->{position},
        tenure => $tenure_hash{$id}->{tenure},
    };
    push @$complete, $complete_row
}
print "complete = " . Dumper($complete);

这应该有效:

my %newHash;
foreach my $arrRef(map {$ooa{$_}} keys %ooa) { #reading all values of ooa hash, each value is an array ref
    foreach my $hashRef(@$arrRef) { #reading each array element, each array element is a hash ref 
        foreach my $key(keys %{$hashRef}) { #reading all keys of each internal hash
            $newHash{$hashRef->{'id'}}{$key} = $hashRef->{$key}; #building new hash of hashes with id as key and value as hash ref
        }
    }
}
my @newArray = map {$newHash{$_}} keys %newHash; #converting hash of hashes into array of hashes

最新更新