9.2 数组的散列
9.2.1 数组的散列的组成
Vsftp:/root/perl/6# cat a1.pl
%HoA = (
flintstones => [ "fred", "barney" ],
jetsons => [ "george", "jane", "elroy" ],
simpsons => [ "homer", "marge", "bart" ],
);
print %HoA;
print "\n";
Vsftp:/root/perl/6# perl a1.pl
simpsonsARRAY(0x1e7c580)jetsonsARRAY(0x1e7c358)flintstonesARRAY(0x1e60d48
要向散列增加另外一个数组,你可以简单地说:
Vsftp:/root/perl/6# cat a1.pl
%HoA = (
flintstones => [ "fred", "barney" ],
jetsons => [ "george", "jane", "elroy" ],
simpsons => [ "homer", "marge", "bart" ],
);
print %HoA;
print "\n";
$HoA{teletubbies} = [ "tinky winky", "dipsy", "laa-laa", "po" ];
print %HoA;
print "\n";
Vsftp:/root/perl/6# perl a1.pl
simpsonsARRAY(0x22fc628)jetsonsARRAY(0x22fc358)flintstonesARRAY(0x22e0d48)
simpsonsARRAY(0x22fc628)teletubbiesARRAY(0x22fc340)jetsonsARRAY(0x22fc358)flintstonesARRAY(0x22e0d48)
9.2.2 生成数组的散列
下面是填充一个数组的散列的技巧,从下面格式的文件中读取出来:
Vsftp:/root/perl/6# cat aa
flintsotnes: fred barney wilma dino
jetsons: george jane elroy
simpsons: homer marge bart
Vsftp:/root/perl/6# cat a2.pl
open (A,aa);
while (<A>){
my @arr=();
my @arr= (split(/:/,$_))[1];
my $str= (split(/:/,$_))[0];
$HoA{$str}=[@arr];
};
print %HoA;
print "\n";
Vsftp:/root/perl/6# perl a2.pl
simpsonsARRAY(0x1c05628)jetsonsARRAY(0x1be9b98)flintsotnesARRAY(0x1be9d48)
Vsftp:/root/perl/6# cat a2.pl
open (A,aa);
while (<A>){
my @arr=();
my @arr= (split(/:/,$_))[1];
my $str= (split(/:/,$_))[0];
$HoA{$str}=[@arr];
};
print %HoA;
print "\n";
print @{$HoA{flintsotnes}};
print "\n";
Vsftp:/root/perl/6# perl a2.pl
simpsonsARRAY(0x2610670)jetsonsARRAY(0x25f4b98)flintsotnesARRAY(0x25f4d48)
fred barney wilma dino
Vsftp:/root/perl/6# cat a2.pl
open (A,aa);
while (<A>){
my @arr=();
my @arr= (split(/:/,$_))[1];
my $str= (split(/:/,$_))[0];
$HoA{$str}=[@arr];
};
print %HoA;
print "\n";
print @{$HoA{flintsotnes}};
print "\n";
Vsftp:/root/perl/6# cat a2.pl
open (A,aa);
while (<A>){
my @arr=();
my @arr= (split(/:/,$_))[1];
my $str= (split(/:/,$_))[0];
$HoA{$str}=[@arr];
};
print %HoA;
print "\n";
print @{$HoA{flintsotnes}};
print "\n";
for $group ( "simpsons", "jetsons", "flintstones" ) {
@members = get_family($group);
$HoA{$group} = [ @members ];
};
print %HoA;
print "\n";
print @{$HoA{flintstones}};
print "\n";
sub get_family(){
my $a=shift;
return @arr=("$a".11,"$a".22,"$a".33);
};
Vsftp:/root/perl/6# perl a2.pl
simpsonsARRAY(0xa3b3c0)jetsonsARRAY(0xa13b98)flintsotnesARRAY(0xa13d48)
fred barney wilma dino
simpsonsARRAY(0xa3b5d0)flintstonesARRAY(0xa3b6c0)jetsonsARRAY(0xa3b690)flintsotnesARRAY(0xa13d48)
flintstones11flintstones22flintstones33
9.2.3 访问和打印数组的散列
你可以用下面的方法设置某一个数组的第一个元素:
Vsftp:/root/perl/6# perl a1.pl
11111111111111111
fred
Vsftp:/root/perl/6# cat a1.pl
%HoA = (
flintstones => [ "fred", "barney" ],
jetsons => [ "george", "jane", "elroy" ],
simpsons => [ "homer", "marge", "bart" ],
);
$HoA{teletubbies} = [ "tinky winky", "dipsy", "laa-laa", "po" ];
print "11111111111111111\n";
print $HoA{flintstones}[0];
print "\n";
Vsftp:/root/perl/6# perl a1.pl
11111111111111111
fred
Vsftp:/root/perl/6# cat a1.pl
%HoA = (
flintstones => [ "fred", "barney" ],
jetsons => [ "george", "jane", "elroy" ],
simpsons => [ "homer", "marge", "bart" ],
);
$HoA{teletubbies} = [ "tinky winky", "dipsy", "laa-laa", "po" ];
print "11111111111111111\n";
print @{$HoA{flintstones}};
print "\n";
$HoA{flintstones}[0] = "Fred";
print @{$HoA{flintstones}};
print "\n";
Vsftp:/root/perl/6# perl a1.pl
11111111111111111
fredbarney
Fredbarney
打印所有这些数组,方法是遍历散列的所有键值:
%HoA = (
flintstones => [ "fred", "barney" ],
jetsons => [ "george", "jane", "elroy" ],
simpsons => [ "homer", "marge", "bart" ],
);
$HoA{teletubbies} = [ "tinky winky", "dipsy", "laa-laa", "po" ];
print %HoA;
print "\n";
for $family ( keys %HoA ){
print "$family: @{ $HoA{$family} }\n";
}
~
Vsftp:/root/perl/6# cat a1.pl
%HoA = (
flintstones => [ "fred", "barney" ],
jetsons => [ "george", "jane", "elroy" ],
simpsons => [ "homer", "marge", "bart" ],
);
$HoA{teletubbies} = [ "tinky winky", "dipsy", "laa-laa", "po" ];
print %HoA;
print "\n";
for $family ( keys %HoA ) {
print "$family: ";
for $i ( 0 .. $#{ $HoA{$family} }) {
print " $i = $HoA{$family}[$i]";
}
print "\n";
}
print %HoA;
print "\n";
Vsftp:/root/perl/6# perl a1.pl
simpsonsARRAY(0xde4a68)teletubbiesARRAY(0xdd9340)jetsonsARRAY(0xdd9358)flintstonesARRAY(0xdbdd48)
simpsons: 0 = homer 1 = marge 2 = bart
teletubbies: 0 = tinky winky 1 = dipsy 2 = laa-laa 3 = po
jetsons: 0 = george 1 = jane 2 = elroy
flintstones: 0 = fred 1 = barney
simpsonsARRAY(0xde4a68)teletubbiesARRAY(0xdd9340)jetsonsARRAY(0xdd9358)flintstonesARRAY(0xdbdd48)
9.2 数组的散列
程序员文章站
2022-03-15 19:39:44
...
上一篇: 2020-11-10