zoukankan      html  css  js  c++  java
  • 9.4 散列的散列

    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 "
    ";
    print $HoH{'flintstones'}->{'pal'};
    print "
    ";
    
    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 "
    ";
    $HoH{ mash } = {
    captain => "pierce",
    major => "burns",
    corporal=> "radar",
    };
    
    print %HoH;
    print "
    ";
    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
    ";
     print "$arr[0] is $arr[0]
    ";
     print "$arr[1] is $arr[1]
    ";
     print "$arr[2] is $arr[2]
    ";
     print "$arr[3] is $arr[3]
    ";
     print "$arr[4] is $arr[4]
    ";
     foreach $field (@arr){
        ($key,$value) = split /=/,$field;
        $HoH{$who}{$key}=$value;
       }
      };
    print Dumper(%HoH);
    print "
    ";
    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
    ";
     print "$arr[0] is $arr[0]
    ";
     print "$arr[1] is $arr[1]
    ";
     print "$arr[2] is $arr[2]
    ";
     print "$arr[3] is $arr[3]
    ";
     print "$arr[4] is $arr[4]
    ";
     foreach $field (@arr){
        ($key,$value) = split /=/,$field;
        $HoH{$who}{$key}=$value;
       }
      };
    print Dumper(%HoH);
    print "
    ";
    print Dumper($HoH{flintstones});
    print "
    ";
    
    ##添加散列成员
    
    %new_floks = (
    xxx => "wilma",
    yyy => "dino",
    );
    for $what (keys %new_floks) {
    $HoH{flintstones}{$what} = $new_floks{$what};
    };
    
    print Dumper($HoH{flintstones});
    print "
    ";
    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'
            };
    

  • 相关阅读:
    Java数据类型转换
    github的入门使用
    移动端的头部标签和meta
    gulp&gulp-less
    前端工程筹建NodeJs+gulp+bower
    jQuery 遍历
    JavaScript for...in 语句
    console.log在线调试
    sessionStorage html5客户端本地存储之sessionStorage及storage事件
    一个页面从输入url到加载完成的过程都发生了什么,请详细说明
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350014.html
Copyright © 2011-2022 走看看