通过哈希值的阵列在Perl遍历一个哈希表 [英] Iterate through Array of Hashes in a Hash in Perl

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

问题描述

我有哈希值的哈希看起来像这样的数组:

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

$VAR1 = {
          'file' => [
                      {
                        'pathname' => './out.log',
                        'size' => '51',
                        'name' => 'out.log',
                        'time' => '1345799296'
                      },
                      {
                        'pathname' => './test.pl',
                        'size' => '2431',
                        'name' => 'test.pl',
                        'time' => '1346080709'
                      },
                      {
                        'pathname' => './foo/bat.txt',
                        'size' => '24',
                        'name' => 'bat.txt',
                        'time' => '1345708287'
                      },
                      {
                        'pathname' => './foo/out.log',
                        'size' => '75',
                        'name' => 'out.log',
                        'time' => '1346063384'
                      }
                    ]
        };

如何通过在一个循环中,这些文件项迭代,并访问其值?一样容易复制我@array = @ {$ {文件列表文件}}; ,所以我只有哈希的数组

How can I iterate through these "file entries" in a loop and access its values? Is it easier to copy my @array = @{ $filelist{file} }; so i only have an array of hashes?

推荐答案

无需复制:

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

这篇关于通过哈希值的阵列在Perl遍历一个哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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