简体   繁体   English

Perl:多维哈希的取消引用数组

[英]Perl: Dereference array of Multidimensional hashes

My overall structure has three layers: 我的总体结构分为三层:

The first layer is a hash: Key :Continent Value : Country 第一层是哈希: :大陆 :国家/地区

The second layer is a hash: Key : Country Value : 2D array with specific countries 第二层是哈希: 关键字 :国家/地区 :具有特定国家/地区的2D数组

The Third layer is a 2D array 第三层是二维数组

So far I have my code below: 到目前为止,我的代码如下:

#!/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;

This is the output: 这是输出:

continent, Asia : ARRAY(0x7fb159824890) ARRAY(0x7fb15a004628) 亚洲大陆:ARRAY(0x7fb159824890)ARRAY(0x7fb15a004628)

continent, America : ARRAY(0x7fb159803ee8) ARRAY(0x7fb1598240e0) 美国大陆:ARRAY(0x7fb159803ee8)ARRAY(0x7fb1598240e0)

My question is: How can I deference the 2D-array in the hash of the hash rather than get their reference?? 我的问题是:如何在散列的散列中引用2D数组而不是获取其引用?

If you have a choice, you should change your data structure's design to what @bipll suggested . 如果可以选择,则应将数据结构的设计更改为@bipll 建议 Your existing data structure's design requires more complex code than necessary. 您现有数据结构的设计需要的代码比必要的复杂。 If you can't, read on. 如果不能,请继续阅读。


The first loop is incorrect. 第一个循环不正确。

This doesn't iterate over the continent names: 这不会遍历各大洲的名称:

foreach my $continent (keys %hash) {

This does: 这样做:

foreach my $continent (keys %{ $hash{continent} }) {

The second loop is incorrect. 第二个循环不正确。

$hash{$continent} now $hash{continent}{$continent} is a reference to an array, not a reference to a hash. $hash{$continent}现在$hash{continent}{$continent}是对数组的引用,而不是对哈希的引用。


Fixed: 固定:

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");
    }
}

First of all, keys %hash is a singleton list ('continent') in your code. 首先, keys %hash在您的代码中是一个单例列表('continent') May I suppose you really wanted continent names as keys? 我是否可以认为您真的希望将大陆名称作为键? Then simply write it as 然后简单地写成

my %hash = (America => [...], Asia => [...]);

Next, 'North' and 'South' look more like regions inside continents, you've probably missed a layer in design description. 接下来,“北部”和“南部”看起来更像是大洲内部的区域,您可能已经错过了设计说明中的一层。

Which suggests something like: 这表明类似:

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"
    }
}

Output: 输出:

America, North, Canada USA
America, South, Peru Brazil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM