3

我的结构如下:

my $var1 = [{a=>"B", c=>"D"}, {E=>"F", G=>"H"}];

现在我想遍历第一个哈希和其中的元素。我该怎么做?

当我做一个倾倒$var1它给我Array时,@var1它说一个哈希。

4

4 回答 4

4

您可以像使用任何其他数组一样迭代该数组,并且您将获得散列引用。然后像使用普通哈希引用一样遍历每个哈希的键。

就像是:

foreach my $hash (@{$var1}) {
  foreach my $key (keys %{$hash}) {
    print $key, " -> ", $hash->{$key}, "\n";
  }
}
于 2013-01-26T10:40:28.790 回答
4

首先,您将使用包含裸字的变量声明来触发 Perl 的严格模式。

考虑到这一点,下面给出了完整的注释示例。

use strict;

my $test = [{'a'=>'B','c'=>'D'},{'E'=>'F','G'=>'H'}];

# Note the @{ $test }
# This says "treat this scalar reference as a list".
foreach my $elem ( @{ $test } ){
    # At this point $elem is a scalar reference to one of the anonymous
    # hashes
    #
    # Same trick, except this time, we're asking Perl
    # to treat the $elem reference as a reference to hash
    #
    # Hence, we can just call keys on it and iterate
    foreach my $key ( keys %{ $elem } ){
        # Finally, another bit of useful syntax for scalar references
        # The "point to" syntax automatically does the %{ } around $elem
        print "Key -> $key = Value " . $elem->{$key} . "\n";
    }
}
于 2013-01-26T10:45:29.403 回答
1
C:\wamp\bin\perl\bin\PERL_2~1\BASIC_~1\REVISION>type traverse.pl
my $var1=[{a=>"B", c=>"D"},{E=>"F", G=>"H"}];
foreach my $var (@{$var1}) {
  foreach my $key (keys(%$var)) {
    print $key, "=>", $var->{$key}, "\n";
  }
  print "\n";
}


C:\wamp\bin\perl\bin\PERL_2~1\BASIC_~1\REVISION>traverse.pl
c=>D
a=>B

G=>H
E=>F

  1. $var1 = []是对匿名数组的引用

  2. 在它之前使用@印记可以$var1让您访问它所引用的数组。所以类似于foreach (@arr) {...}你会做foreach (@{$var1}) {...}的。

  3. 现在,您提供的数组中的元素@{$var1}也是匿名的(意味着未命名),但它们是匿名哈希,所以就像使用 arrayref 一样,在这里我们%{$hash_reference}可以访问由$hash_reference. 这里,$hash_reference$var

  4. 使用 访问散列后,使用或%{$var}访问散列的键变得很容易。由于返回的结果是一个键数组,因此我们可以使用inside 。keys(%$var)keys(%{$var})keys(%{$var})foreach (keys(%{$var})) {...}

  5. 我们通过使用类似的键来访问匿名哈希中的标量值$hash_reference->{$keyname},这就是代码所做的全部。

  6. 如果您的数组包含数组的匿名哈希,例如: $var1=[ { akey=>["b", "c"], mkey=>["n", "o"]} ]; 那么,这就是您访问数组值的方式:

    C:\wamp\bin\perl\bin\PERL_2012\BASIC_PERL\REVISION>type traverse.pl
    my $var1=[ {akey=>["b", "c"], mkey=>["n", "o"]} ];
    
    foreach my $var (@{$var1}) {
      foreach my $key (keys(%$var)) {
        foreach my $elem (@{ $var->{$key} }) {
          print "$key=>$elem,";
        }
        print "\n...\n";
      }
      print "\n";
    }
    C:\wamp\bin\perl\bin\PERL_2012\BASIC_PERL\REVISION>traverse.pl
    mkey=>n,mkey=>o,
    ...
    akey=>b,akey=>c,
    ...
    

经常练习,很快你就会很容易将复杂的结构分解成这样的组合。这就是我为另一个软件创建大型解析器的方式,它充满了您问题的答案:)

于 2013-01-26T11:13:39.163 回答
0

看了一眼上面 amon 投票赞成的评论(谢谢,amon!)我能够写出这个小曲子:

#!/usr/bin/perl
# Given an array of hashes, print out the keys and values of each hash.

use strict; use warnings;
use Data::Dump qw(dump);

my $var1=[{A=>"B",C=>"D"},{E=>"F",G=>"H"}];
my $count = 0;

# @{$var1} is the array of hash references pointed to by $var1
foreach my $href (@{$var1})
{
    print "\nArray index ", $count++, "\n";
    print "=============\n";

    # %{$href} is the hash pointed to by $href
    foreach my $key (keys %{$href})
    {
            # $href->{$key} ( ALT: $$href{$key} ) is the value
            # corresponding to $key in the hash pointed to by
            # $href
            # print $key, " => ", $href->{$key}, "\n";
            print $key, " => ", $$href{$key}, "\n";
    }

print "\nCompare with dump():\n";
dump ($var1);

print "\nJust the first hash (index 0):\n";
# $var1->[0] ( ALT: $$var1[0] ) is the first hash reference (index 0)
# in @{$var1}
# dump ($var1->[0]);
dump ($$var1[0]);

#print "\nJust the value of key A: \"", $var1->[0]->{A}, "\"\n";
#print "\nJust the value of key A: \"", $var1->[0]{A}, "\"\n";
print "\nJust the value of key A: \"", $$var1[0]{A}, "\"\n"
于 2013-01-27T00:25:20.377 回答