取消引用Perl中的哈希数组?

时间:2013-07-01 18:12:15

标签: perl dereference

数据存储在@emailaddresses中,如下所示:

$VAR1 = { 'email' => 'abc@google.com' };
$VAR2 = { 'email' => 'cde@google.com' };
$VAR3 = { 'email' => 'efg@google.com' };
$VAR4 = { 'email' => 'hij@google.com' };

当我打印@emailaddresses(print @emailaddresses)时,它会打印HASH(0x...)而不是数组中的值。如何打印值?

1 个答案:

答案 0 :(得分:1)

只需遍历您的阵列并打印出每个地址的“电子邮件”值:

my @emailaddresses = ...;

for my $addr ( @emailaddresses ) {
   print $addr->{email}, "\n";
}

更好的是,将此逻辑包装在一个函数中:

sub print_email_addresses {
   my @addresses = @_;

   for my $addr ( @addresses ) {
      print $addr->{email}, "\n";
   }
}