这是你的结构:
{
Fred => {
"Street Name" => ["First Avenue"],
"animal" => ["lion", "snake", "spider", "monkey"],
},
Dave => {
"Street Name" => ["Church Street"],
"animal" => ["dog", "cat", "pig", "elephant"],
},
}
让我们一次把它拆开一个层次。这里分为三个层次:
外层代表我将调用的散列%person_hash
。您的哈希中有两个键:Fred
和Dave
. 这两个散列值中的每一个都指向(参考)其他散列值。那是$person_hash{Dave}
一个哈希引用,$person_hash{Fred}
是一个哈希引用。
要将这两个哈希引用转换为哈希,我使用解引用语法:
%attribute_hash = %{ $person_hash{Dave} };
现在,我们有一个名为%attribute_hash
. 这%attribute_hash
包含 Fred 和 Dave 的属性。在您的示例中,每个%attribute_hash
散列中有两个元素(请记住:Dave 一个,Fred 一个)。这些%attribute_hash
哈希中的两个键控元素包含“街道地址”和“动物”。
要访问列表,我可以使用取消引用语法:@values = @{ $attribute_hash{$attribute} }
.
那么,让我们看看如何打印所有这些:
use strict;
use warnings;
use feature qw(say);
my %person_hash = (
Fred => {
"Street Name" => [ "First Avenue" ],
animal => [ "lion", "snake", "spider", "monkey" ],
},
Dave => {
"Street name" => [ "Church Street" ],
animal => [ "dog", "cat", "pig", "elephant" ],
},
);
# The first level contains the keys `Dave` and `Fred`
for my $person ( keys %person_hash ) {
say "The person is $person";
# The keys will be for "Dave" and "Fred", and will be the value
# of $person. This is a hash reference, so let's dereference it.
my %attribute_hash = %{ $person_hash{$person} };
# We have a hash of attributes beloning to that person. The
# attributes will be "Street Name" and "animal"
for my $attribute ( keys %attribute_hash ) {
say " ${person}'s attribute is '$attribute'";
# Each "attribute" points to a list. Let's get the list
my @value_list = @{ $attribute_hash{$attribute} };
# Now we can go through that list:
for my $value ( @value_list ) {
say " ${person}'s attribute '$attribute' has a value of $value";
}
}
}
这打印出来:
The person is Dave
Dave's attribute is 'Street name'
Dave's attribute 'Street name' has a value of Church Street
Dave's attribute is 'animal'
Dave's attribute 'animal' has a value of dog
Dave's attribute 'animal' has a value of cat
Dave's attribute 'animal' has a value of pig
Dave's attribute 'animal' has a value of elephant
The person is Fred
Fred's attribute is 'Street Name'
Fred's attribute 'Street Name' has a value of First Avenue
Fred's attribute is 'animal'
Fred's attribute 'animal' has a value of lion
Fred's attribute 'animal' has a value of snake
Fred's attribute 'animal' has a value of spider
Fred's attribute 'animal' has a value of monkey
->
您还应该知道,我可以使用以下语法直接访问其中的值:
say "Fred's first animal in his list is " . $person_hash{Fred}->{animal}->[0];
当我取消引用时,我也可以使用该语法:
say "Fred's animals are " . join ", ", @{ $person_hash->{Fred}->{animal} };
请注意,这$person_hash->{Fred}->{animal}
是对包含动物的数组的引用。