欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

9.3 散列的数组:

程序员文章站 2022-03-15 19:37:56
...
9.3 散列的数组:

Vsftp:/root/perl/6# cat a3.pl 
@AoH = (
{
husband => "barney",
wife => "betty",
son => "bamm bamm",
},
{
husband => "george",
wife => "jane",
son => "elroy",
},
{
husband => "homer",
wife => "marge",
son => "bart",
},
);

print @AoH;
print "\n";
print $AoH[0]->{son};
print "\n";
Vsftp:/root/perl/6# perl a3.pl 
HASH(0x1302d48)HASH(0x131f6d8)HASH(0x131f750)
bamm bamm

要向数组中增加另外一个散列,你可以简单地说:
Vsftp:/root/perl/6# cat a3.pl 
@AoH = (
{
husband => "barney",
wife => "betty",
son => "bamm bamm",
},
{
husband => "george",
wife => "jane",
son => "elroy",
},
{
husband => "homer",
wife => "marge",
son => "bart",
},
);

print @AoH;
print "\n";
print $AoH[0]->{son};
print "\n";
push @AoH, { husband => "fred", wife => "wilma", daughter => "pebbles" };
print @AoH;
print "\n";
Vsftp:/root/perl/6# perl a3.pl 
HASH(0x1dbdd48)HASH(0x1de5590)HASH(0x1de5608)
bamm bamm
HASH(0x1dbdd48)HASH(0x1de5590)HASH(0x1de5608)HASH(0x1dda408)



9.3.2 生成散列的数组:

Vsftp:/root/perl/6# cat a4.pl 
open (A,bb);
while (<A>){
 my @arr=();
 my  @arr= split(/\s+/,$_);
 print "\@arr is @arr\n";

 ##定义散列引用
 my $rec={};
 for $field (@arr){
    ($key, $value) = split /=/, $field;
     $rec->{$key} = $value;
     }
  push @AoH, $rec;
  print @AoH;
  print "\n";
 
};


print "1111111111111\n";
print @AoH;
print "\n";
print %{$AoH[0]};
print "\n";
print %{$AoH[1]};
print "\n";


Vsftp:/root/perl/6# cat bb
husband=fred friend=barney
aa=bb    cc=dd

Vsftp:/root/perl/6# perl a4.pl 
@arr is husband=fred friend=barney
HASH(0x25bed48)
@arr is aa=bb cc=dd
HASH(0x25bed48)HASH(0x25beb98)
1111111111111
HASH(0x25bed48)HASH(0x25beb98)
friendbarneyhusbandfred
ccddaabb


/****************************************

Vsftp:/root/perl/6# cat a10.pl 
open (A,cc);
while (<A>){
     ##创建hash引用
     $rec={};
     my @arr=split(/\s+/,$_);
     foreach (@arr){
     ($key, $value) = split /=/, $_;
     $rec->{$key} = $value; 
    };
    };
 %h= %{$rec};
 print $h{friend};
 print "\n";
 print $h{husband};
 print "\n";
  



Vsftp:/root/perl/6# cat cc
husband=fred friend=barney
Vsftp:/root/perl/6# perl a10.pl 
barney
fred


Vsftp:/root/perl/6# cat a11.pl 
use Data::Dumper;
open (A,cc);
while (<A>){
     ##创建hash引用
     $rec={};
     my @arr=split(/\s+/,$_);
     foreach (@arr){
     ($key, $value) = split /=/, $_;
     $rec->{$key} = $value; 
    };
   push @AoH ,$rec;
    };
  
print Dumper(@AoH);
print "\n";
print $AoH[0]->{husband};
print "\n";



Vsftp:/root/perl/6# perl a11.pl 
$VAR1 = {
          'friend' => 'barney',
          'husband' => 'fred'
        };

fred


9.3.3 访问和打印散列的数组 

你可以用下面的方法设置一个特定散列的数值/键值对

Vsftp:/root/perl/6# cat a11.pl 
use Data::Dumper;
open (A,cc);
while (<A>){
     ##创建hash引用
     $rec={};
     my @arr=split(/\s+/,$_);
     foreach (@arr){
     ($key, $value) = split /=/, $_;
     $rec->{$key} = $value; 
    };
   push @AoH ,$rec;
    };
  
print Dumper(@AoH);
print "\n";
print $AoH[0]->{husband};
print "\n";
$AoH[0]{pet} = "dino";
print Dumper(@AoH);



Vsftp:/root/perl/6# perl a11.pl 
$VAR1 = {
          'friend' => 'barney',
          'husband' => 'fred'
        };

fred
$VAR1 = {
          'pet' => 'dino',
          'friend' => 'barney',
          'husband' => 'fred'
        };


你可以用下面的方法打印所有的数据:

##遍历每个引用
for $href (@AoH){
         ##%{$href} 解引用,打印键和值
         for $key (keys %{$href}){
           print "$key---$href->{$key}";
           print "\n";
           }
};