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

[经验分享] Perl之哈希与列表

[复制链接]

尚未签到

发表于 2015-12-27 10:41:22 | 显示全部楼层 |阅读模式
[自用]
先引用一段话来简单介绍为什么用‘引用’以及什么是‘引用’:
    “当Perl进化到Perl 5时,我们必须考虑到哈希原来的设计:哈希的键值必须是标量, 一个'引用'就是一个指向一个完整列表或完整哈希(或指向其他的东西,比如函数)的标量值。Perl中的'引用'就像列表和哈希的名字。它们是Perl中私有的,内部的名字,所以你可以确信它们是明确的, 一个'引用'只指向一个东西。你总是可以知道它指向什么。如果你有一个指向数组的'引用',你可以从它恢复出整个数组。如果你有一个指向哈希的'引用',你可以根据它恢复出整个哈希。但是这个'引用'仍旧是一个简单、紧凑的标量。你不能使用一个健值是数组的哈希;哈希的健值必须是标量。我们被这个束缚住了。但是一个简单的'引用'能指向一个完整的数组,'引用'是标量,所以你可以使用指向数组的'引用'组成的哈希,它就像一个数组的哈希一样,和数据的哈希一样有用。”
Firstly, we create an array and a hash seperately for example:
my @array1 = (10, 20, 30, 40, 50);
my %hash1 = (abc=>123, def=>456);

Then, we create a reference for each of them(you may get familiar with them here:), they are much like the Point in C/C++, aren't they?):
$aref1 = /@array1;      
$href1 = /%hash1;   
At this point, we could save both of the references into other scalars:
$foo = $aref
$var = $herf
Sometimes, you could see definition of an array or hash like:
$aref2 = [10,20,30,40,50];     # [] not ()
$href2 = {abc=>123, def=>456}; # {} not ()
we call them anonymous array and anoymous hash here, anonymous array or hash returns a reference directly. Thus we could see that $aref1 and $aref2($href1 and $href2) are actually equivalent, both of them are scalar.
How do we use them? i guess that the most usual case is to use them in for/foreach iteration:
--- for/foreach: @array1 & %hash1
# @array1:
# for...
for my $element (@array1) {
    print $element;
}
for (my $i=0;$i<@arr;$i++) {
    print $arr[$i];
}
# foreach...
foreach (@array1) {
    print $_;
}
foreach my $element(@array1) {
    print $element;
}
#%hash1:
#for...
for my $key (keys %hash1) {
    print $hash1{$key};
}
#foreach...
foreach $ele (sort keys %hash1) {
    my @foo = @{$hash1{$ele}};
    print join ',',sort @foo;
    print "\n";
}
--- for/foreach: $aref1 and $href1, $aref2 and $href2
Here we'll talk about the anoymous array and hash.
# $aref1, $aref2
#for...
for my $element (@{$aref1}) {
    print $element;
}
for(my $i=0; $i<@{$aref1};$i++) {
    print $aref1->[$i]; # it's more beautiful than ${$aref1}[$i], isn't it:)?
}
# $href1, $href2
# for...
for my $key (keys %{$href1}) {
    print $href1->{$key}; # and it's better than ${$href1}{$key} :)
}
I Guess you may become confident after the content above, at least, you can use the reference of array and hash in your program right now.
Here we go play some more...
multi-dimension array(we'll take 2D for example)
Assuming we have a 2D array which is:
@array_2d = (
[1,2,3,4],
[5,6,7,8],
)
A question will be how we goona pick out '7' from the 2D matrix?
you might guess $array_2d[1][2], then congratulation, however, it's more clear to use $array_2d[1]->[2] like we said above.

how about the case if we have an anonymous multi-dimentsion array? how to reap '7'?
$array_3d_ref = [
[1,2,3,4],
[5,6,7,8],
];
if you give an answer like ${$array_3d_ref}[1]->[2], then Congratulation, you can rest easy...
Below is a little example, it might not be a good one, however, it can give you some verification on what we just learned from the post, and most importantly, you could think about how to make a modification on it to verify what you just learned:P

##################################################################
use strict;
use Data::Dumper;

use constant M => "m";
use constant W01 => "w01";
use constant W02 => "w02";
open(FILE,"test.dat");
my %meminfo = (
M => [],
W01 => [],
W02 => [],
);
#local $/= "\n";
while (<FILE>) {
chomp;
next if /^\s*$/;
my $spec= $_;
my @temp_array = split(/\|/);
if ($temp_array[1] eq M) {
push(@{$meminfo{M}}, $temp_array[3]);29 } elsif ($temp_array[1] eq W01) {
push(@{$meminfo{W01}}, $temp_array[3]);32 } else {
push(@{$meminfo{W02}}, $temp_array[3]);35 }
}

print Dumper($meminfo{M});
print Dumper($meminfo{W01});
print Dumper($meminfo{W02});

close(FILE);

Reference:
1. http://www.iyunv.com/old_jh/25/504623.html
  

运维网声明 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-156869-1-1.html 上篇帖子: 写个perl程序自动下载《南方周末》(2005年12月最后一期,38版,值得一看) 下篇帖子: 用perl脚本实现一个简单的行注释到块注释的转换
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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