これがあなたの構造です:
{
Fred => {
"Street Name" => ["First Avenue"],
"animal" => ["lion", "snake", "spider", "monkey"],
},
Dave => {
"Street Name" => ["Church Street"],
"animal" => ["dog", "cat", "pig", "elephant"],
},
}
これを 1 レベルずつ分解してみましょう。ここには 3 つのレベルがあります。
外側のレベルは、私が呼び出すハッシュを表します%person_hash
。ハッシュには と の 2 つのキーがFred
ありDave
ます。これら 2 つのハッシュのそれぞれの値は、他のハッシュをポイント (参照) します。これは$person_hash{Dave}
ハッシュ参照で$person_hash{Fred}
あり、ハッシュ参照です。
これら 2 つのハッシュ参照をハッシュに変換するには、逆参照構文を使用します。
%attribute_hash = %{ $person_hash{Dave} };
これで、 というハッシュができました%attribute_hash
。これ%attribute_hash
には Fred と Dave の属性が含まれます。あなたの例では、これらのハッシュのそれぞれに 2 つの要素が%attribute_hash
あります (覚えておいてください: Dave 用と Fred 用に 1 つずつあります)。これらの%attribute_hash
ハッシュの 2 つのキー付き要素には、「Street Address」と「animal」が含まれています。
リストにアクセスするには、逆参照構文を使用できます: @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}
であることに注意してください。