Perl 复杂数据结构

作者: 214b3ff96d82 | 来源:发表于2019-08-20 20:07 被阅读1次

    01. 举个二维哈希的例子

    #!/usr/bin/perl
    
    use warnings;
    use strict;
    use Data::Dumper qw(Dumper);
    
    #creating 2D hash
    my %company = ('Sales' =>    { 
                                    'Brown' => 'Manager', 
                                    'Smith' => 'Salesman', 
                                    'Albert' => 'Salesman',  
                                },  
                'Marketing' =>  { 
                                    'Penfold' => 'Designer', 
                                    'Evans' => 'Tea-person', 
                                    'Jurgens' => 'Manager',  
                                }, 
                'Production' => { 
                                    'Cotton' => 'Paste-up', 
                                    'Ridgeway' => 'Manager', 
                                    'Web' => 'Developer',  
                                }, 
                );  
    # print the list
    print Dumper \%company;
    

    OUTPUT

    $VAR1 = {
              'Marketing' => {
                               'Evans' => 'Tea-person',
                               'Jurgens' => 'Manager',
                               'Penfold' => 'Designer'
                             },
              'Sales' => {
                           'Smith' => 'Salesman',
                           'Albert' => 'Salesman',
                           'Brown' => 'Manager'
                         },
              'Production' => {
                                'Ridgeway' => 'Manager',
                                'Web' => 'Developer',
                                'Cotton' => 'Paste-up'
                              }
            };
    
    • Dumper函数可以将复杂结构展示出来

    02. 向哈希添加元素

    $company{'new_key'} = {
        'sub_key1' => value1,
        'sub_key2' => value2,
        'sub_key3' => value3
    }
    

    03.读取特定值

    print $company{"Production"}{"Web"}
    # will output "Developer"
    

    04.设置特定键的值

    $company{$Production}->{"Web"} =  Senior Developer
    # changes Web to Senior Developer
    

    05.遍历多维哈希

    若要遍历多维散列或遍历散列中的每个值,只需遍历外部散列的键,然后遍历内部散列的键。对于n维哈希,需要n个嵌套循环或嵌入循环遍历完整的哈希。for和while循环都可以用来循环到散列。

    for $key (keys %hash) 
    { 
        print "$key: \n"; 
        for $ele (keys %{$hash{$key}}) 
        { 
            print " $ele: " . $hash{$key}->{$ele} . "\n"; 
        } 
    } 
    

    对于大型多维散列,使用“each”同时检索键和值可能会稍微快一些。

    while (($key, $ele) = each %hash) 
    { 
        print "$key: \n"; 
        while (($ele, $sub_ele) = each %$ele) 
        { 
            print " $ele = $sub_ele "; 
        } 
        print "\n"; 
    } 
    

    下面的示例演示如何使用for和while循环遍历多维哈希:

    use strict;
    use warnings;
    use Data::Dumper
    
    my %company = ('Sales' =>    { 
                                    'Brown' => 'Manager', 
                                    'Smith' => 'Salesman', 
                                    'Albert' => 'Salesman',  
                                },  
                'Marketing' =>  { 
                                    'Penfold' => 'Designer', 
                                    'Evans' => 'Tea-person', 
                                    'Jurgens' => 'Manager',  
                                }, 
                'Production' => { 
                                    'Cotton' => 'Paste-up', 
                                    'Ridgeway' => 'Manager', 
                                    'Web' => 'Developer',  
                                }, 
                );  
    print "Traversing hash using For loop: "."\n"; 
    print "\n"; 
      
    # traversing hash using for loop 
    for my $key (keys %company) {
        print "$key\n";
        for my $ele (keys %{$company{$key}}){
            print " $ele: " . $company{$key}->{$ele} . "\n";
     }
    } 
    
    print "\nTraversing hash using while" .  
          "loop using each keyword: " . "\n"; 
    print "\n"; 
      
    # traversing hash using each keyword 
    # and while loop 
    while ((my $key, my $ele) = each %company) { 
        print "$key: \n"; 
        while ((my $ele, my $sub_ele) = each %$ele)  { 
            print " $ele = $sub_ele "; 
        } 
            print "\n"; 
    } 
    
    

    output

    Traversing hash by For loop.
    
    Marketing: 
     Evans: Tea-person
     Jurgens: Manager
     Penfold: Designer
    Sales: 
     Brown: Manager
     Albert: Salesman
     Smith: Salesman
    Production: 
     Cotton: Paste-up
     Web: Developer
     Ridgeway: Manager
    
    Traversing hash using while loop with each keyword.
    
    Marketing: 
     Evans=Tea-person  Jurgens=Manager  Penfold=Designer 
    Sales: 
     Brown=Manager  Albert=Salesman  Smith=Salesman 
    Production: 
     Cotton=Paste-up  Web=Developer  Ridgeway=Manager 
    
    

    06.检测多维哈希的键值

    很多时候,当使用perl哈希时,我们需要知道哈希中是否已经存在某个键。给定一个散列,就可以通过使用exists关键字来检查特定密钥的存在。在上面示例中使用的像%Company这样的多维哈希中,必须一直使用关键字exists,直到检查到是否存在的键的深度级别为止。

    if (exists($hash{key})) {
    if (exists($hash{key}{sub_key})) {
    ….
    }
    }
    

    下面是一个演示Perl存在散列函数的简单示例。在这个Perl脚本中,我们首先创建一个简单的Perl哈希,然后使用exists函数查看哈希中是否存在名为‘Albert’的哈希键。

    # !/usr/bin/perl  
    use strict; 
    use warnings; 
    my %company = ('Sales' => { 
                                    'Brown' => 'Manager', 
                                    'Smith' => 'Salesman', 
                                    'Albert' => 'Salesman', 
                                }, 
                'Marketing' => { 
                                    'Penfold' => 'Designer', 
                                    'Evans' => 'Tea-person', 
                                    'Jurgens' => 'Manager', 
                                }, 
                'Production' => { 
                                    'Cotton' => 'Paste-up', 
                                    'Ridgeway' => 'Manager', 
                                    'Web' => 'Developer', 
                                }, 
                ); 
    
    # Check for key existence 
    if (exists $company{"Sales"}) { 
        print "Sales department exists.\n"; 
        if (exists $company{"Sales"}{"Albert"}) { 
            print "Albert is " . $company{"Sales"}{"Albert"} . 
                " of Sales department . \n"; 
        } 
        else { 
            print "Albert is not a member of Sales department.\n"; 
        } 
    } 
    else { 
        print "Sales department do not exists.\n"; 
    } 
    
    # output
    
    Sales department exists.
    Albert is Salesman of Sales department.
    

    07.从文件读取存入多维哈希

    flintstones: husband=fred pal=barney wife=wilma pet=dino

    while (<>){
        next unless s/^(.*?):\s*//;
        $who = $1;
        for $field (split) {
            ($key, $value) = split /=/, $field;
            $HoH{$who}{$key} = $value
      }
    }
    
    whike (<>) {
        next unless s/^(.*?):\s*//;
        $who = $1;
        $rec = {};
        $HoH{$who} = $rec;
        for $field (split) {
             ($key, $value) = split /=/, $field;
              $rec -> {$key} = $value;
     }
    
    }
    

    相关文章

      网友评论

        本文标题:Perl 复杂数据结构

        本文链接:https://www.haomeiwen.com/subject/yebrsctx.html