1

我有以下 hashrefs 结构的 hashref:

$hashref = {
    user1 => {
        key1 => "unique_value1",
        ...
        key99 => "value1_99"
    },
    ...
    user26 => {
        key1 => "unique_value2",
        ...
        key99 => "value1_99"
    },

    user99 => {
        key1 => "unique_value1",
        ...
        key99 => "value99_99"
    },
};

我想要的:

$hashref = {
    "unique_value1" => ["user1","user99"],
    "unique_value2" => ["user26"]
};

我历来在创建时构建了倒置地图,但我越来越懒了。

我可以用任何一种糖来做这个吗?

谢谢你。

4

2 回答 2

2

单程:

my $h; 
push (@{$h->{$hashref->{$_}{key1}}}, $_) for keys %$hashref;
于 2013-01-11T02:01:01.870 回答
0
my %users_by_uval;
for my $user_id (%$users) {
    push @{ $users_by_uval{ $users->{$user_id}{key1} } }, $user_id;
}
于 2013-01-11T01:59:53.413 回答