结合2+';深';perl中的(多维)散列
有一个问题确切地解释了我在这里想要什么: 然而,那里的答案似乎不适合我(建议使用结合2+';深';perl中的(多维)散列,perl,hash,merge,hash-of-hashes,Perl,Hash,Merge,Hash Of Hashes,有一个问题确切地解释了我在这里想要什么: 然而,那里的答案似乎不适合我(建议使用合并模块) 我有两个类似的散列: $VAR1 = { '57494' => { 'name' => 'John Smith', 'age' => '9', 'height' => '120' },
合并
模块)
我有两个类似的散列:
$VAR1 = {
'57494' => {
'name' => 'John Smith',
'age' => '9',
'height' => '120'
},
'57495' => {
'name' => 'Amy Pond',
'age' => '17',
'height' => '168'
}
}
};
$VAR1 = {
'57494' => {
'name_address' => 'Peter Smith',
'address' => '5 Cambridge Road',
'post_code' => 'CR5 0FS'
}
}
};
如果我使用Hash::Merge
或%c={%a,%b}
格式,我每次都会得到以下结果:
$VAR1 = '57494';
$VAR2 = {
'name_address' => 'Peter Smith',
'address' => '5 Cambridge Road',
'post_code' => 'CR5 0FS'
};
(因此它基本上用第二个数据重写了第一个数据,并弄乱了键)当我需要时:
$VAR1 = {
'57494' => {
'name' => 'John Smith',
'age' => '9',
'height' => '120'
'name_address' => 'Peter Smith',
'address' => '5 Cambridge Road',
'post_code' => 'CR5 0FS'
},
'57495' => {
'name' => 'Amy Pond',
'age' => '17',
'height' => '168'
}
}
};
因此,当键相同时,数据会合并在一起,否则新键只会附加到末尾。我希望这是有意义的。也许我在使用Merge
时做了一些错误的事情,或者需要“手动”将它们添加到循环中,但无论如何,我花了太多时间来考虑它
编辑:如何使用“合并”查看我是否在做傻事:
我有:
use Hash::Merge qw( merge );
...hash data above as %hash1 and %hash2...
my %combined_hash = %{ merge( %hash1,%hash2 ) };
print Dumper(%combined_hash);
根据我上次喝的咖啡是否好,我会或多或少地戴上牙套
Python显然更适合这种情况,因为它不会让您考虑引用:
for key in fromdict:
if key not in todict:
todict[key] = {}
todict[key] = dict(fromdict[key].items() + todict[key].items())
或者如果todict
是defaultdict
(在读取和分配时创建键):
如果我使用参考资料,它就像一个符咒
use strict; use warnings;
use Data::Dumper;
use Hash::Merge qw(merge);
my $h1 = {
'57494' => {
'name' => 'John Smith',
'age' => '9',
'height' => '120'
},
'57495' => {
'name' => 'Amy Pond',
'age' => '17',
'height' => '168'
}
};
my $h2 = {
'57494' => {
'name_address' => 'Peter Smith',
'address' => '5 Cambridge Road',
'post_code' => 'CR5 0FS'
}
};
my $h3 = merge( $h1, $h2 );
print Dumper $h3;
输出:
$VAR1 = {
'57495' => {
'name' => 'Amy Pond',
'age' => '17',
'height' => '168'
},
'57494' => {
'name_address' => 'Peter Smith',
'name' => 'John Smith',
'post_code' => 'CR5 0FS',
'address' => '5 Cambridge Road',
'height' => '120',
'age' => '9'
}
};
但是,如果我使用散列而不是散列引用,则不会:
my %hash1 = (
'57494' => {
'name' => 'John Smith',
'age' => '9',
'height' => '120'
},
'57495' => {
'name' => 'Amy Pond',
'age' => '17',
'height' => '168'
}
);
my %hash2 = (
'57494' => {
'name_address' => 'Peter Smith',
'address' => '5 Cambridge Road',
'post_code' => 'CR5 0FS'
}
);
my %h3 = merge( %hash1, %hash2 );
print Dumper \%h3;
__END__
$VAR1 = {
'57495' => undef
};
这是因为merge
from只能获取引用,但您正在传递散列。此外,您需要在标量上下文中调用它
试着这样做:
# +--------+--- references
# ,-- SCALAR context | |
my $combined_hash = %{ merge( \%hash1, \%hash2 ) };
print Dumper($combined_hash);
我不。我使用或获得您想要的结果。示例数据中的
}
太多…如果有,则是复制粘贴错误。我是从Dumper打印出来的。@Axeman如果对你有用,我可以问一下你是怎么做到的吗?如果你知道……看看我对@Axeman可能做了什么的回答,那会很有用。我猜他只是看了文件;-)我没有咖啡参考。那么你也没有喝足够的咖啡。事实上,第5行还需要一个括号:keys%{$fromhash{$key}})
。不过我还是要喝约克郡茶@Phil H,您的perl代码运行得非常好。但是我不能理解第三行。我的理解是,如果密钥存在于%fromhash
中,并且在%tohash
中不存在,则它会分配一个空的匿名哈希。但是,它正在做正确的事情,将该键添加到%tohash
,我错了。那么你能解释一下第三行的逻辑吗。谢谢。如果%tohash
中不存在$key
,在循环的内部中对该散列的分配将失败。因此,如果它不是通过分配一个空的散列引用来定义的,我们就创建它。啊,你知道,我有我的原始版本作为引用,但是这样做了:my%h3=%{merge($h1,$h2)}但是当我把它改为纯引用时,它似乎起作用了。奇怪的我觉得有点傻,我错过了尝试这种组合,但感谢你让我明白@Bladepartera这里面有个错误。键(不是散列键)是引用两个参数散列,而是在标量上下文中调用它。您需要的是$combined\u hash
,而不是%combined\u hash
。然后,您可以引用传递给merge
的散列,而不需要将它们放入引用变量中。
my %hash1 = (
'57494' => {
'name' => 'John Smith',
'age' => '9',
'height' => '120'
},
'57495' => {
'name' => 'Amy Pond',
'age' => '17',
'height' => '168'
}
);
my %hash2 = (
'57494' => {
'name_address' => 'Peter Smith',
'address' => '5 Cambridge Road',
'post_code' => 'CR5 0FS'
}
);
my %h3 = merge( %hash1, %hash2 );
print Dumper \%h3;
__END__
$VAR1 = {
'57495' => undef
};
# +--------+--- references
# ,-- SCALAR context | |
my $combined_hash = %{ merge( \%hash1, \%hash2 ) };
print Dumper($combined_hash);