如何在Perl中的哈希中遍历哈希数组 [英] How to iterate through Array of Hashes in a Hash in Perl

查看:977
本文介绍了如何在Perl中的哈希中遍历哈希数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哈希中有一个哈希数组,看起来像这样:

I have an Array of Hashes in a Hash that looks like this:

$var  = {
      'items' => [
                      {
                        'name'  => 'name1',
                        'type'  => 'type1',
                        'width' => 'width1',
                      },
                      {
                        'name'  => 'name2',
                        'type'  => 'type2',
                        'width' => 'width2',
                      },
                      {
                        'name'  => 'name3',
                        'type'  => 'type3',
                        'width' => 'width3',
                      }                      
                   ]
    };

我编写了以下代码来从文件中获取值.

I wrote the following code to get the values from a file.

my @members = ("name"    =>  $name,
               "type"    =>  $type,
               "width"   =>  $width);

$rec->{$items} = [ @members ];

push @var, $rec;

我不确定如何从此数据结构中检索值.

I am not sure how to retrieve the values from this data structure.

我在中看到了解决方案在Perl中的哈希中遍历哈希数组. 但是我不明白.我不确定他们在代码中提到的$ filelist是什么.

I saw the solution in Iterate through Array of Hashes in a Hash in Perl. But I did not understand it. I am not sure what was $filelist that they had mentioned in the code.

foreach my $file (@{ $filelist{file} }) {
    print "path: $file->{pathname}; size: $file->{size}; ...\n";
}

我是perl的新手,请在您的回复中向我提供详细信息.

I am new to perl and please provide me details in your reply.

推荐答案

也就是说,这是代码:

for my $item (@{$aoh->{items}}) {
    # '@' casts the $aoh->{items} hash references to an array
    print $item->{name};
}

这篇关于如何在Perl中的哈希中遍历哈希数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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