如何在一个哈希引用其他哈希的地方打印哈希哈希

时间:2014-02-27 03:21:35

标签: perl

我正在尝试在perl中学习复杂的数据结构,因为我已经编写了代码,但我没有得到任何输出:

#!/usr/bin/perl 
use strict;
use warnings;

my %abc=(Education => "BE",Marital_Status => "Single", Age => "28", Working_Exp => "4yrs");
my %cde=(Education => "BE",Marital_Status => "Single", Age => "29", Working_Exp => "5yrs");


my %info =(info_one => "\%abc", info_two => "\%cde");

foreach my $val (keys %info)
{
  foreach my $check ( keys %{$info{val}})
  {
    print ${$info{val}}{check}."\n";
  }
}

1 个答案:

答案 0 :(得分:5)

为了在Perl中学习复杂的数据结构,没有比Perl Data Structures Cookbook更好的参考。

info_oneinfo_two are字符串的分配,因此您要删除\%abc\%cde周围的双引号。此外,您需要将$标量符号添加到最后val行中的checkprint,因为这些是变量。

#!/usr/bin/perl 
use strict;
use warnings;

my %abc= (
    Education => "BE",
    Marital_Status => "Single", 
    Age => "28", 
    Working_Exp => "4yrs"
);
my %cde= (
    Education => "BE",
    Marital_Status => "Single", 
    Age => "29", 
    Working_Exp => "5yrs"
);


my %info = (
   info_one => \%abc,
   info_two => \%cde
);

foreach my $val (keys %info) {
    foreach my $check ( keys %{$info{$val}} ) {
        print ${$info{$val}}{$check}."\n";
    }
}

最后一行有点难看,但在阅读数据结构手册时,您将学会使用->运算符并更优雅地编写语句。