简体   繁体   English

在Perl中的哈希中迭代哈希数组

[英]Iterate through Array of Hashes in a Hash in 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'
                      }
                    ]
        };

How can I iterate through these "file entries" in a loop and access its values? 如何在循环中迭代这些“文件条目”并访问其值? Is it easier to copy my @array = @{ $filelist{file} }; 是否更容易复制my @array = @{ $filelist{file} }; so i only have an array of hashes? 所以我只有一系列哈希?

No need to copy: 无需复制:

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

There are no arrays of hashes in Perl, only arrays of scalars. Perl中没有哈希数组,只有标量数组。 It only happens that there's a bunch of syntactic sugar in case those scalars are references to arrays or hashes. 只有在这些标量引用数组或散列的情况下才会出现一堆语法糖。

In your example, $VAR1 holds a reference to a hash containing a reference to an array containing references to hashes. 在您的示例中,$ VAR1保存对哈希的引用,该哈希包含对包含对哈希的引用的数组的引用。 Yeah, that's quite a lot of nesting to deal with. 是的,这是一个很大的嵌套处理。 Plus, the outer hash seems kinda useless, since it contains only one value. 另外,外部哈希看起来有点无用,因为它只包含一个值。 So yes, I think giving the inner array a meaningful name would definitely make things clearer. 所以,是的,我认为给内部数组一个有意义的名称肯定会让事情更加清晰。 It's not actually a "copy": only the reference is copied, not the contents. 它实际上不是“副本”:只复制引用,而不是内容。 All of the following are equivalent: 以下所有内容均相同:

my @files = $VAR1 -> {file} # dereferencing with the -> operator
my @files = ${$VAR1}{file}  # derefencing with the sigil{ref} syntax
my @files = $$VAR1{file}    # same as above with syntactic sugar

Note that when using the sigil{ref} syntax, the sigil obeys the same rules as usual: %{$ref} (or %$ref ) is the hash referenced by $ref, but the element of %{$ref} for a given key is ${$ref}{key} (or $$ref{key} ). 请注意,当使用sigil {ref}语法时,sigil遵循与往常相同的规则: %{$ref} (或%$ref )是%$ref的哈希值,但%{$ref}的元素是给定key${$ref}{key} (或$$ref{key} )。 The braces can contain arbitrary code returning a reference, while the short version can only be used when a scalar variable already holds the reference. 大括号可以包含返回引用的任意代码,而短版本只能在标量变量已经保存引用时使用。

Once your array of references to hashes is in a variable, iterating over it is as easy as: 一旦你的哈希引用数组在一个变量中,迭代它就像下面这样简单:

for (@files) {
    my %file = %$_;
    # do stuff with %file
}

See: http://perldoc.perl.org/perlref.html 请参阅: http//perldoc.perl.org/perlref.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM