从 Perl 中的嵌套数据结构中提取非同级哈希值数组
这是我由 Data::Dumper->Dumper 创建的数据结构:(
$VAR1 = {
'name' => 'genomic',
'class' => [
{
'reference' => [
{
'name' => 'chromosome',
'referenced-type' => 'Chromosome'
},
{
'name' => 'chromosomeLocation',
'referenced-type' => 'Location'
},
{
'name' => 'sequence',
'referenced-type' => 'Sequence'
},
{
'name' => 'sequenceOntologyTerm',
'referenced-type' => 'SOTerm'
}
],
}
],
};
为了清晰起见,进行了修剪)
我想在单行中返回对引用下的每个名称值的数组的引用。
目前我有
$class->[0]{reference}[0..3]{name}
但无济于事。
此外,此示例有四个索引为 0..3 的同级哈希,如何独立于元素数量来表示整个数组?
This is my data structure created by Data::Dumper->Dumper:
$VAR1 = {
'name' => 'genomic',
'class' => [
{
'reference' => [
{
'name' => 'chromosome',
'referenced-type' => 'Chromosome'
},
{
'name' => 'chromosomeLocation',
'referenced-type' => 'Location'
},
{
'name' => 'sequence',
'referenced-type' => 'Sequence'
},
{
'name' => 'sequenceOntologyTerm',
'referenced-type' => 'SOTerm'
}
],
}
],
};
(trimmed for clarity)
I would like to return a reference to an array of each name value under reference in a single line.
Currently I have
$class->[0]{reference}[0..3]{name}
but no avail.
Also this example has four sibling-hashes with indexes 0..3, how can I represent the whole array independent of the number of elements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,没有一个简单的语法可以做到这一点。您必须使用 map:
然后,如果您转储 $array_ref,您将看到它包含:
如果您需要对原始字符串(而不是副本)的引用,则只需在
$_ (所以它会是地图内的
\$_->{name}
)。There isn't an easy syntax to do that, unfortunately. You'll have to use map:
Then, if you dump out $array_ref, you'll see it contains:
If you need references to the original strings (not copies), you just need a backslash before
$_
(so it'd be\$_->{name}
inside the map).$class->[0]{reference}
是一个数组引用,所以你必须用@{}
取消引用它:是'整个数组',你可以然后在末尾使用切片语法来获取其中的一部分:
从那里您将使用哈希引用数组,因此您必须使用
for
或map< 对其进行迭代/代码>。
$class->[0]{reference}
is an array reference, so you have to dereference it with@{}
:Is the 'whole array', you can then use slice syntax on the end to get a part of it:
From there you're working with an array of hashrefs, so you'll have to iterate over it with
for
ormap
.