2014-02-27 44 views
0

我想学习Perl中复杂的数据结构,对于II已经写了代码,但我没有得到任何输出:如何打印哈希散列在一个哈希是参考其他

#!/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"; 
    } 
} 
+0

使用匿名哈希将帮助您存储和管理数据。 –

+0

@NaghaveerRGowda:你的意思是,使用“[hashrefs](http://stackoverflow.com/questions/1817394/whats-the-difference-between-a-hash-and-hash-reference-in-perl)”,以及你想要更精确地说“会帮助你存储和管理数据”:) –

+1

@DanDascalescu:是它的“hashrefs”和谢谢丹:) –

回答

5

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

info_oneinfo_two are字符串的任务,所以要各地\%abc\%cde去除双引号。另外,您需要在最后的print行中将$标量符号添加到valcheck,因为这些是变量。

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

最后一行是有点丑陋,但你通过数据结构食谱阅读,您将学习如何使用->操作和更优雅写的语句。