zoukankan      html  css  js  c++  java
  • hash 和hash引用的常见操作方法

    hash和hash引用的常见操作方法如下:

    #init a hash 
    my %hash = ();
    
    #init a hash reference 
    my $hash_ref = {};
    
    # add a key/value pair to hash
    $hash{"key01"} = '111';
    $hash{"key02"} = '222';
    
    $hash_ref->{"key01"} = "333";
    $hash_ref->{"key02"} = "444";
    
    # loop hash array
    while (my ($key, $value) = each(%hash)){
        print "$key => $value
    ";
    }
    
    print "
    ";
    
    while (my ($key, $value) = each(%$hash_ref)){
        print "$key => $value
    ";
    }
    print "
    ";
    
    #get hash size-ref
    
    print "hash size : ", scalar(keys(%hash)),"
    ";
    print "hash ref size: ", scalar(keys %$hash_ref),"
    ";

    运行结果:

    key02 => 222
    key01 => 111
    
    key02 => 444
    key01 => 333
    
    hash size : 2
    hash ref size: 2

    更多操作方法可以参加Perl Hash How to

  • 相关阅读:
    js
    DOM
    css
    html
    java虚拟机
    java并发
    java容器
    java基础
    计算机网络面试题
    计算机操作系统-设备管理
  • 原文地址:https://www.cnblogs.com/jinxiang1224/p/8468168.html
Copyright © 2011-2022 走看看