设为首页 收藏本站
查看: 723|回复: 0

[经验分享] Perl中对hash的操作

[复制链接]

尚未签到

发表于 2015-12-25 16:02:50 | 显示全部楼层 |阅读模式
Perl Hash Howto
  This how-to comes with no guaratees other than the fact that these code segments were copy/pasted from code that I wrote and ran successfully.


  

Initialize (clear, or empty) a hash
  Assigning an empty list is the fastest method.
  Solution

    my %hash = ();
  

Initialize (clear, or empty) a hash reference
  People have asked how to initialize a hash reference (aka hash ref and href). This is the way to go:
  Solution

    my $hash_ref = {};  # a reference to an empty hash, ref will return HASH
  The great thing about this is that if before performing an actual assignment, you want to determine (using the ref operator) the type of thingy that a reference is pointing to, you can!... and you can expect it to be a HASH built-in type, because that is what the line above initializes it to be.
  Note
  If you treat the variable just as any scalar variable; and use the my declaration alone, or assign a value, ref will return false.

    my $hash_ref;
my $hash_ref = 0;  # zero
  

Add a key/value pair to a hash
  In the solutions below, quotes around the keys can be omitted when the keys are identifiers.
  Hash:
  Solution

    $hash{ 'key' } = 'value';    # hash
    $hash{ $key } = $value;      # hash, using variables
  Hash reference:
  Solution

    $href->{ 'key' } = 'value';  # hash ref
    $href->{ $key } = $value;    # hash ref, using variables
  

Add several key/value pairs to a hash
  Solution
  The following statements are equivalent, though the second one is more readable:

    %hash = ( 'key1', 'value1', 'key2', 'value2', 'key3', 'value3' );
    %hash = (
key1 => 'value1',
key2 => 'value2',
key3 => 'value3',
);
  

Copy a hash
  Solution

    my %hash_copy = %hash;  # copy a hash
    my $href_copy = $href;  # copy a hash ref
  

Delete a single key/value pair
  The solution differs for a hash and a hash reference, but both cases can use the delete function.
  Solution
  Hash:

    delete $hash{$key};
  Hash reference:

    delete $hash_ref->{$key};
  

Perform an action on each key/value pair in a hash
  The actions below print the key/value pairs.
  Solution
  Use each within a while loop. Note that each iterates over entries in an apparently random order, but that order is guaranteed to be the same for the functions keys and values.

    while ( my ($key, $value) = each(%hash) ) {
print "$key => $value\n";
}
  A hash reference would be only slightly different:

    while ( my ($key, $value) = each(%$hash_ref) ) {
print "$key => $value\n";
}
  Solution
  Use keys with a for loop.

    for my $key ( keys %hash ) {
my $value = $hash{$key};
print "$key => $value\n";
}
  Example

    my $file = $ARGV[0] || "-";
my %from = ();
open FILE, "< $file" or die "Can't open $file : $!";
while( <FILE> ) {
if (/^From: (.*)/) { $from{$1}++ }  # count recurrences of sender
}
close FILE;
for my $sender ( sort keys %from ) {
print "$sender: $from{$sender}\n";
}
  

Get the size of a hash
  Solution

    print "size of hash:  " . keys( %hash ) . ".\n";
  Solution

    my $i = 0;
$i += scalar keys %$hash_ref;  # method 1: explicit scalar context
$i += keys %$hash_ref;         # method 2: implicit scalar context
  

Use hash references
  Solution

    sub foo
{
my $hash_ref;
$hash_ref->{ 'key1' } = 'value1';
$hash_ref->{ 'key2' } = 'value2';
$hash_ref->{ 'key3' } = 'value3';
return $hash_ref;
}
my $hash_ref = foo();
print "the keys... ", sort keys %$hash_ref, "...\n";
  

Create a hash of hashes; via references
  The following two solutions are equivalent, except for the way the look. In my opinion the second approach is clearer.
  Solution

    $requiredPatches_href->{ $patch }->{ os }    = $os;
$requiredPatches_href->{ $patch }->{ arch }  = $arch;
$requiredPatches_href->{ $patch }->{ info }  = $info;
  Solution

    $requiredPatches_href->{ $patch } = {
os    => $os,
arch  => $arch,
info  => $info,
};
  

Function to build a hash of hashes; return a reference
  Solution

    sub foo
{
my ( $login, $p, $uid, $gid, $gecos, $dir, $s );
my %HoH = ();
my $file = '/etc/passwd';
open( PASSWD, "< $file" ) or die "Can't open $file : $!";
while( <PASSWD> ) {
( $login, $p, $uid, $gid, $gecos, $dir, $s ) = split( ':' );
$HoH{ $login }{ 'uid' } = $uid;
$HoH{ $login }{ 'gid' } = $gid;
$HoH{ $login }{ 'dir' } = $dir;
}
close PASSWD;
return \%HoH;
}
  

Access and print a reference to a hash of hashes
  Solution

    my $rHoH = foo();
my( $uid, $gid, $dir );
for my $login ( keys %$rHoH ) {
$uid =       $rHoH->{ $login }->{ 'uid' };   # method 1  most readable
$gid =    ${ $rHoH->{ $login } }{ 'gid' };   # method 2
$dir = ${ ${ $rHoH }{ $login } }{ 'dir' };   # method 3 least readable
print "uid: $uid, gid: $gid, dir, $dir.\n";
}
  Solution

    my $rHoH = foo();
for my $k1 ( sort keys %$rHoH ) {
print "k1: $k1\n";
for my $k2 ( keys %{$rHoH->{ $k1 }} ) {
print "k2: $k2 $rHoH->{ $k1 }{ $k2 }\n";
}
}
  

Function to build a hash of hashes of hashes; return a reference
  Solution

    sub foo
{
my %HoHoH = ();
while( ... ) {
if( /LOCATION:/ ) {
...
} elsif( /MODULE:/ ) {
$HoHoH{ $loc }{ $module_type }{ MODULE_NAME } = $module_name;
} elsif( $ARGS_ALLOWED ) {
$HoHoH{ $loc }{ $module_type }{ $arg_name } = $arg_value;
}
}
return \%HoHoH;
}
  

Access and print a reference to a hash of hashes of hashes
  Solution

    my $rHoHoH = foo();
for my $k1 ( sort keys %$rHoHoH ) {
print "$k1\n";
for my $k2 ( sort keys %{$rHoHoH->{ $k1 }} ) {
print "\t$k2\n";
for my $k3 ( sort keys %{$rHoHoH->{ $k1 }->{ $k2 }} ) {
print "\t\t$k3 => $rHoHoH->{ $k1 }->{ $k2 }->{ $k3 }\n";
}
}
}
  

Print the keys and values of a hash, given a hash reference
  Solution

    while( my ($k, $v) = each %$hash_ref ) {
print "key: $k, value: $v.\n";
}
  

Determine whether a hash value exists, is defined, or is true
  Solution

    print "Value EXISTS, but may be undefined.\n" if exists  $hash{ $key };
print "Value is DEFINED, but may be false.\n" if defined $hash{ $key };
print "Value is TRUE at hash key $key.\n"     if         $hash{ $key };
  Example
  Let's say we execute an sql query where some of the resulting values may be NULL. Before attempting to use any of the values we should first check whether they are defined, as in the following code. Note that the subroutine sql_fetch_hashref() takes care of connecting to the database, preparing the statement, executing it, and returning the resulting row as a hash reference using DBI's fetchrow_hashref() method.

    my $answers = 'a,b,c,d,e';
my $sql = "select max_time, $answers from questions " .
'where question_number=?';
my $hash_ref = sql_fetch_hashref( $sql, $q );
my @answers = split ',', $answers;
my $max_time = $hash_ref->{max_time} || '60';
my $hash_ref_ans;
for my $letter ( @answers ) {
$hash_ref_ans->{ $letter } = $hash_ref->{ $letter }
if defined $hash_ref->{ $letter };
}
  The for loop made a new hash of only defined key/value pairs.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156320-1-1.html 上篇帖子: Perl解析当当网图书信息页面 下篇帖子: PERL unicode
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表