9.4 散列的散列
多维的散列是perl里面最灵活的嵌套结构,它就好像绑定一个记录,该记录本身包含其他记录。
9.4.1 构成一个散列的散列
你可以用下面方法创建一个匿名散列的散列:
Vsftp:/root/perl/6# cat a12.pl
%HoH = (
flintstones => {
husband => "fred",
pal => "barney",
},
jetsons => {
husband => "george",
wife => "jane",
"his boy" => "elroy", # 键字需要引号
},
simpsons => {
husband => "homer",
wife => "marge",
kid => "bart",
},
);
print %HoH;
print "\n";
print $HoH{'flintstones'}->{'pal'};
print "\n";
Vsftp:/root/perl/6# perl a12.pl
simpsonsHASH(0x1cf3750)jetsonsHASH(0x1cf3408)flintstonesHASH(0x1cd6d48)
barney
要向%HoH 中增加另外一个匿名散列,你可以简单地说:
$HoH{ mash } = {
captain => "pierce",
major => "burns",
corporal=> "radar",
};
Vsftp:/root/perl/6# cat a12.pl
%HoH = (
flintstones => {
husband => "fred",
pal => "barney",
},
jetsons => {
husband => "george",
wife => "jane",
"his boy" => "elroy", # 键字需要引号
},
simpsons => {
husband => "homer",
wife => "marge",
kid => "bart",
},
);
print %HoH;
print "\n";
$HoH{ mash } = {
captain => "pierce",
major => "burns",
corporal=> "radar",
};
print %HoH;
print "\n";
Vsftp:/root/perl/6# perl a12.pl
simpsonsHASH(0x25ca700)jetsonsHASH(0x25c3408)flintstonesHASH(0x25a6d48)
mashHASH(0x25c33f0)simpsonsHASH(0x25ca700)jetsonsHASH(0x25c3408)flintstonesHASH(0x25a6d48)
9.4.2 生成散列的散列:
Vsftp:/root/perl/6# cat a13.pl
use Data::Dumper;
my $who="flintstones";
open (A,"dd");
while (<A>){
my @arr=split(/\s+/,$_);
print "\@arr is @arr\n";
print "\$arr[0] is $arr[0]\n";
print "\$arr[1] is $arr[1]\n";
print "\$arr[2] is $arr[2]\n";
print "\$arr[3] is $arr[3]\n";
print "\$arr[4] is $arr[4]\n";
foreach $field (@arr){
($key,$value) = split /=/,$field;
$HoH{$who}{$key}=$value;
}
};
print Dumper(%HoH);
print "\n";
Vsftp:/root/perl/6# perl a13.pl
@arr is husband=fred pal=barney wife=wilma pet=dino
$arr[0] is husband=fred
$arr[1] is pal=barney
$arr[2] is wife=wilma
$arr[3] is pet=dino
$arr[4] is
$VAR1 = 'flintstones';
$VAR2 = {
'pet' => 'dino',
'wife' => 'wilma',
'husband' => 'fred',
'pal' => 'barney'
};
添加散列成员:
Vsftp:/root/perl/6# cat a13.pl
use Data::Dumper;
my $who="flintstones";
open (A,"dd");
while (<A>){
my @arr=split(/\s+/,$_);
print "\@arr is @arr\n";
print "\$arr[0] is $arr[0]\n";
print "\$arr[1] is $arr[1]\n";
print "\$arr[2] is $arr[2]\n";
print "\$arr[3] is $arr[3]\n";
print "\$arr[4] is $arr[4]\n";
foreach $field (@arr){
($key,$value) = split /=/,$field;
$HoH{$who}{$key}=$value;
}
};
print Dumper(%HoH);
print "\n";
print Dumper($HoH{flintstones});
print "\n";
##添加散列成员
%new_floks = (
xxx => "wilma",
yyy => "dino",
);
for $what (keys %new_floks) {
$HoH{flintstones}{$what} = $new_floks{$what};
};
print Dumper($HoH{flintstones});
print "\n";
Vsftp:/root/perl/6# perl a13.pl
@arr is husband=fred pal=barney wife=wilma pet=dino
$arr[0] is husband=fred
$arr[1] is pal=barney
$arr[2] is wife=wilma
$arr[3] is pet=dino
$arr[4] is
$VAR1 = 'flintstones';
$VAR2 = {
'pet' => 'dino',
'wife' => 'wilma',
'husband' => 'fred',
'pal' => 'barney'
};
$VAR1 = {
'pet' => 'dino',
'wife' => 'wilma',
'husband' => 'fred',
'pal' => 'barney'
};
$VAR1 = {
'pet' => 'dino',
'wife' => 'wilma',
'xxx' => 'wilma',
'husband' => 'fred',
'yyy' => 'dino',
'pal' => 'barney'
};