通过哈希值的阵列在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";
}