我正在尝试在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";
}
}
答案 0 :(得分:5)
为了在Perl中学习复杂的数据结构,没有比Perl Data Structures Cookbook更好的参考。
info_one
和info_two are
字符串的分配,因此您要删除\%abc
和\%cde
周围的双引号。此外,您需要将$
标量符号添加到最后val
行中的check
和print
,因为这些是变量。
#!/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";
}
}
最后一行有点难看,但在阅读数据结构手册时,您将学会使用->
运算符并更优雅地编写语句。