以下子例程用于迭代节点数组(每个节点的哈希值都有一个大陆)并返回所有国家/地区的列表:
sub getContinentsServed{
my $self = shift;
my $temp = $self->{cityListRef};
my %hash = {};
my $h_ref = \%hash;
foreach my $cont (@{$temp}){
$h_ref->{$cont->{continent}} = '1';
}
print "Continents Served: ";
foreach my $coord (keys %hash){
print $coord;
print " , ";
}
}
Run Code Online (Sandbox Code Playgroud)
我很确定这包含所有正确的数据,但是当我尝试打印这些值时,我得到了这个结果:
Continents Served: Australia Europe North America South America Asia Africa HASH(0x100949a70)
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么我会得到最后一个元素HASH(0x1009...).我宁愿这不会出现,但我所做的只是迭代键,所以我不确定为什么或如何那样.