我的结构如下:
my $var1 = [{a=>"B", c=>"D"}, {E=>"F", G=>"H"}];
现在,我想遍历第一个哈希及其中的元素。我该怎么做?
[当我执行$var1
的转储时,它会给我Array
,当在@var1
上时,它会显示哈希。
您可以像在其他数组上那样遍历该数组,并且将获得哈希引用。然后像使用普通哈希引用一样遍历每个哈希的键。
类似:
foreach my $hash (@{$var1}) {
foreach my $key (keys %{$hash}) {
print $key, " -> ", $hash->{$key}, "\n";
}
}
首先,您将使用包含裸词的变量声明来触发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";
}
}
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
$var1 = []
是对匿名数组的引用
使用@
之前的$var1
标记使您可以访问它所引用的数组。与foreach (@arr) {...}
相似,您可以执行foreach (@{$var1}) {...}
。
现在,您提供的数组@{$var1}
中的元素也是匿名的(意味着未命名),但它们也是匿名的哈希,因此就像arrayref一样,在这里我们执行%{$hash_reference}
来访问$hash_reference
引用的哈希。这里,$hash_reference
是$var
。
使用%{$var}
访问哈希后,使用keys(%$var)
或keys(%{$var})
访问哈希的键变得容易。由于返回的结果是键数组,因此我们可以在keys(%{$var})
中使用foreach (keys(%{$var})) {...}
。
[我们访问标量值在匿名哈希中,使用$hash_reference->{$keyname}
之类的键,这就是所有代码。
如果您的数组包含数组的匿名哈希,例如:$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,
...
更多且有规律地练习,您很快就可以将复杂的结构分解为这样的组合。这就是我创建large parser for another software的方式,它充满了您的问题的答案:)
上面偷看了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"