zoukankan      html  css  js  c++  java
  • 9.3 散列的数组:

    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 "
    ";
    print $AoH[0]->{son};
    print "
    ";
    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 "
    ";
    print $AoH[0]->{son};
    print "
    ";
    push @AoH, { husband => "fred", wife => "wilma", daughter => "pebbles" };
    print @AoH;
    print "
    ";
    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
    ";
    
     ##定义散列引用
     my $rec={};
     for $field (@arr){
        ($key, $value) = split /=/, $field;
         $rec->{$key} = $value;
         }
      push @AoH, $rec;
      print @AoH;
      print "
    ";
     
    };
    
    
    print "1111111111111
    ";
    print @AoH;
    print "
    ";
    print %{$AoH[0]};
    print "
    ";
    print %{$AoH[1]};
    print "
    ";
    
    
    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 "
    ";
     print $h{husband};
     print "
    ";
      
    
    
    
    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 "
    ";
    print $AoH[0]->{husband};
    print "
    ";
    
    
    
    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 "
    ";
    print $AoH[0]->{husband};
    print "
    ";
    $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 "
    ";
               }
    };
    

  • 相关阅读:
    C++总结
    KMP算法
    理解I/O Completion Port(完成端口)
    [转载]Windows网络编程系列教程之四:Select模型
    [转载]理解 I/O Completion Port (IOCP完成端口)
    [转载]IOCP模型的总结
    发个IOCP的C++例子
    说说网络通信模型
    几个网络模型的示例代码(BlockingModel、OverlappedModel、WSAEventSelect、CompletionRoutine)..c++
    关于Socket 多线程 的一篇好文章
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350015.html
Copyright © 2011-2022 走看看