比较公共密钥合并值的多个哈希值 [英] compare multiple hashes for common keys merge values

查看:184
本文介绍了比较公共密钥合并值的多个哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作位的代码在这里,我比较六个哈希的键,以找到所有的共同的那些。然后我将每个散列的值合并为一个新的散列中的值。我想做的是使这可缩放。我想能够轻松地从比较3哈希到100,而不必回到我的代码和改变它。任何想法,我将如何实现这一点?

I have a working bit of code here where I am comparing the keys of six hashes together to find the ones that are common amongst all of them. I then combine the values from each hash into one value in a new hash. What I would like to do is make this scaleable. I would like to be able to easily go from comparing 3 hashes to 100 without having to go back into my code and altering it. Any thoughts on how I would achieve this? The rest of the code already works well for different input amounts, but this is the one part that has me stuck.

my $comparison = List::Compare->new([keys %{$posHashes[0]}], [keys %{$posHashes[1]}], [keys %{$posHashes[2]}], [keys %{$posHashes[3]}], [keys %{$posHashes[4]}], [keys %{$posHashes[5]}]);
my %comboHash;
for ($comparison->get_intersection) {
$comboHash{$_} = ($posHashes[0]{$_} . $posHashes[1]{$_} . $posHashes[2]{$_} . $posHashes[3]{$_} . $posHashes[4]{$_} . $posHashes[5]{$_});
}


推荐答案

my %all;
for my $posHash (@posHashes) {
   for my $key (keys(%$posHash)) {
      push @{ $all{$key} }, $posHash->{$key};
   }
}

my %comboHash;
for my $key (keys(%all)) {
   next if @{ $all{$key} } != @posHashes;
   $comboHash{$key} = join('', @{ $all{$key} });
}

这篇关于比较公共密钥合并值的多个哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆