[英]How do I append a new hash to an array of hashes?
If I wanted to add a new hash to all the arrays in the mother_hash
using a loop, what would be the syntax? 如果我想使用循环向
mother_hash
中的所有数组添加新哈希,那么语法是什么?
My hash: 我的哈希:
my %mother_hash = (
'daughter_hash1' => [
{
'e' => '-4.3',
'seq' => 'AGGCACC',
'end' => '97',
'start' => '81'
}
],
'daughter_hash2' => [
{
'e' => '-4.4',
'seq' => 'CAGT',
'end' => '17',
'start' => '6'
},
{
'e' => '-4.1',
'seq' => 'GTT',
'end' => '51',
'start' => '26'
},
{
'e' => '-4.1',
'seq' => 'TTG',
'end' => '53',
'start' => '28'
}
],
#...
);
First I would point out the daughter hashes aren't hashes but arrays of anonymous hashes. 首先我要指出女儿的哈希不是哈希,而是匿名哈希的数组。 To add another daughter hash:
要添加另一个女儿哈希:
$mother_hash{daughter_hash3} = [ { %daughter_hash3 } ];
This creates an anonymous array that contains an anonymous hash with the contents of %daughter_hash3
. 这将创建一个匿名数组,其中包含一个内容为
%daughter_hash3
的匿名哈希。
For a loop: 对于一个循环:
$mother_hash{$daughter_hash_key} = [ { %daughter_hash } ];
where $daughter_hash_key
is a string contain the key for the %mother_hash
and %daughter_hash
is the hash to add. 其中
$daughter_hash_key
是一个字符串,包含%mother_hash
的键, %daughter_hash
是要添加的哈希。
To add another hash to a daughter array with key $daughter_hash_key
: 要使用键
$daughter_hash_key
将另一个哈希添加到子数组:
push @{ $mother_hash{$daughter_hash_key} }, { %daughter_hash };
I know ti's complicated but I suggest you use Data::Dumper
to dump the contents of %mother_hash
each time thru the loop to see if it grows correctly. 我知道这很复杂但我建议你每次通过循环时使用
Data::Dumper
转储%mother_hash
的内容,看它是否正确生长。
use Data::Dumper;
print Dumper \%mother_hash;
See perldoc Data::Dumper
for details.. 有关详细信息,请参阅
perldoc Data::Dumper
。
Data::Dumper
is a standard module that comes with Perl. Data::Dumper
是Perl附带的标准模块。 For a list of standard modules, see perldoc perlmodlib
. 有关标准模块的列表,请参阅
perldoc perlmodlib
。
If you have a hash of arrays of hashes and want to add a new hash to the end of each of the arrays, you can do: 如果你有哈希数组的哈希,并想在每个数组的末尾添加一个新哈希,你可以这样做:
push @{ $_ }, \%new_hash for (values %mother_hash);
This loop iterates over the values of %mother_hash
(which are array refs in this case) and setting $_
for each iteration. 此循环遍历
%mother_hash
的值(在本例中为数组引用)并为每次迭代设置$_
。 Then in each iteration, we push the reference to the new hash %new_hash
to the end of that array. 然后在每次迭代中,我们将对新哈希值
%new_hash
的引用推送到该数组的末尾。
mother_hash
is a hash of arrays of hashes. mother_hash
是哈希数组的哈希值。
To add another top-level array of hashes. 添加另一个顶级哈希数组。
%mother_hash{$key} = [ { stuff }, { stuff } ];
To add another entry to an existing array 向现有阵列添加另一个条目
push @{%mother_hash{'key'}} { stuff };
To add another entry to the hash in the embedded array 向嵌入数组中的哈希添加另一个条目
%{@{%mother_hash{'top_key'}}[3]}{'new_inner_key'} = value;
When confused and attempting to match up the "types" of hash / array / scalar containing a hash reference / array reference, you can use the following technique 当混淆并尝试匹配包含哈希引用/数组引用的哈希/数组/标量的“类型”时,您可以使用以下技术
use Data::Dumper;
$Data::Dumper::Terse = 1;
printf("mother_hash reference = %s\n", Dumper(\%mother_hash));
printf("mother_hash of key 'top_key' = %s\n", Dumper(%mother_hash{top_key}));
and so on to find your way through a large data structure and validate that you are narrowing down to the region you want to access or alter. 等等,找到通过大型数据结构的方法,并验证您是否正在缩小到要访问或更改的区域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.