我有以下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"]
};
我历史上在创建时构建了倒置贴图,但我变得很懒惰。
我可以使用任何一线糖吗?
谢谢。
答案 0 :(得分:2)
一种方式:
my $h;
push (@{$h->{$hashref->{$_}{key1}}}, $_) for keys %$hashref;
答案 1 :(得分:0)
my %users_by_uval;
for my $user_id (%$users) {
push @{ $users_by_uval{ $users->{$user_id}{key1} } }, $user_id;
}