Perl:取消引用多维哈希数组
Perl: Dereference array of Multidimensional hashes
我的整体结构分为三层:
第一层是哈希:Key:Continent Value:Country
第二层是哈希:Key:国家Value:具有特定国家的二维数组
第三层是一个二维数组
到目前为止我的代码如下:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper qw(Dumper);
my %hash=(); #initialize a hash
my $hash_ref = \%hash;
$hash{continent} = {
America => [
['North', 'Canada', 'USA'],
['South', 'Peru', 'Brazil'],
],
Asia => [
['East', 'Japan', 'China'],
['Sounth', 'India','Bangladesh'],
],
};
foreach my $continent (keys %hash) {
foreach my $country (keys %{ $hash{$continent} }) {
print "$continent, $country : @{$hash{$continent}{$country}}\n";
}
}
#print Dumper \%hash;
这是输出:
大陆、亚洲:ARRAY(0x7fb159824890) ARRAY(0x7fb15a004628)
美洲大陆:ARRAY(0x7fb159803ee8) ARRAY(0x7fb1598240e0)
我的问题是:
如何在散列的散列中引用二维数组而不是获取它们的引用?
首先,keys %hash
是您代码中的单例列表 ('continent')
。我可以假设您真的想要大陆名称作为键吗?然后干脆写成
my %hash = (America => [...], Asia => [...]);
接下来,'North' 和 'South' 看起来更像是大陆内部的区域,您可能错过了设计描述中的一层。
这表明类似:
my %hash = (America => {North => ['Canada', 'USA'], South => ['Peru', 'Brazil']});
for my $continent (keys %hash) {
for my $region (keys %{$hash{$continent}}) {
print "$continent, $region, @{$hash{$continent}{$region}}\n"
}
}
输出:
America, North, Canada USA
America, South, Peru Brazil
如果您有选择,您应该将数据结构的设计更改为@bipll 。您现有数据结构的设计需要比必要的更复杂的代码。如果不能,请继续阅读。
第一个循环不正确。
这不会遍历大陆名称:
foreach my $continent (keys %hash) {
这样做:
foreach my $continent (keys %{ $hash{continent} }) {
第二个循环不正确。
$hash{$continent}
现在 $hash{continent}{$continent}
是对数组的引用,而不是对散列的引用。
固定:
my $by_continent = $hash{continent};
for my $continent_name (keys(%$by_continent)) {
my $continent = $by_continent->{$continent_name};
for my $region (@$continent) {
my $region_name = $region->[0];
my @country_names = @{$region}[0..$#$region];
print("$continent_name, $region_name: ", join(', ', @country_names), "\n");
}
}
我的整体结构分为三层:
第一层是哈希:Key:Continent Value:Country
第二层是哈希:Key:国家Value:具有特定国家的二维数组
第三层是一个二维数组
到目前为止我的代码如下:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper qw(Dumper);
my %hash=(); #initialize a hash
my $hash_ref = \%hash;
$hash{continent} = {
America => [
['North', 'Canada', 'USA'],
['South', 'Peru', 'Brazil'],
],
Asia => [
['East', 'Japan', 'China'],
['Sounth', 'India','Bangladesh'],
],
};
foreach my $continent (keys %hash) {
foreach my $country (keys %{ $hash{$continent} }) {
print "$continent, $country : @{$hash{$continent}{$country}}\n";
}
}
#print Dumper \%hash;
这是输出:
大陆、亚洲:ARRAY(0x7fb159824890) ARRAY(0x7fb15a004628)
美洲大陆:ARRAY(0x7fb159803ee8) ARRAY(0x7fb1598240e0)
我的问题是: 如何在散列的散列中引用二维数组而不是获取它们的引用?
首先,keys %hash
是您代码中的单例列表 ('continent')
。我可以假设您真的想要大陆名称作为键吗?然后干脆写成
my %hash = (America => [...], Asia => [...]);
接下来,'North' 和 'South' 看起来更像是大陆内部的区域,您可能错过了设计描述中的一层。
这表明类似:
my %hash = (America => {North => ['Canada', 'USA'], South => ['Peru', 'Brazil']});
for my $continent (keys %hash) {
for my $region (keys %{$hash{$continent}}) {
print "$continent, $region, @{$hash{$continent}{$region}}\n"
}
}
输出:
America, North, Canada USA
America, South, Peru Brazil
如果您有选择,您应该将数据结构的设计更改为@bipll
第一个循环不正确。
这不会遍历大陆名称:
foreach my $continent (keys %hash) {
这样做:
foreach my $continent (keys %{ $hash{continent} }) {
第二个循环不正确。
$hash{$continent}
现在 $hash{continent}{$continent}
是对数组的引用,而不是对散列的引用。
固定:
my $by_continent = $hash{continent};
for my $continent_name (keys(%$by_continent)) {
my $continent = $by_continent->{$continent_name};
for my $region (@$continent) {
my $region_name = $region->[0];
my @country_names = @{$region}[0..$#$region];
print("$continent_name, $region_name: ", join(', ', @country_names), "\n");
}
}