Perl之哈希与列表

摘要:
[自用]首先,我们将简要介绍为什么使用“引用”以及什么是“引用”:“当Perl5发展到Perl5时,我们必须考虑哈希的原始设计:哈希的关键值必须是标量,而“引用”是对完整列表或完整哈希(或其他东西,如函数)的引用的标量值。'Perl中的References类似于列表和散列的名称。它们是Perl中的私有和内部名称,因此您可以确保它们是显式的。“引用”只指向一件事。你总能知道它指向什么。如果
[自用]
先引用一段话来简单介绍为什么用‘引用’以及什么是‘引用’:
“当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.chinaunix.net/old_jh/25/504623.html

免责声明:文章转载自《Perl之哈希与列表》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇AG-Admin微服务框架入门[转载]Linux C编程-实现文件夹的递归拷贝下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

AR的一些常见的操作

一、查询数据集合 1、$admin=Admin::model()->findAll($condition,$params); 该方法是根据一个条件查询一个集合,如:   findAll("username=:name",array(":name"=>$username));      2、$admin=Admin::model()->...

php实现MySQL两库对比升级版

define('DATABASE1', 'db1'); $dbi1 = new DbMysql; $dbi1->dbh = 'mysql://root:password@127.0.0.1/'.DATABASE1; define('DATABASE2', 'db2'); $dbi2 = new DbMysql; $dbi2->dbh = 'm...

echarts 加单位 加数字单位

formatter: function (array) { return array.seriesName + "<br/>" + array.marker + array.name + "平均体重" + array.value + "KG" } 完整代码: <div id="Cha...

ThinkPHP3快速入门教程三:查询语言

一、查询语言简介: ThinkPHP内置了非常灵活的查询方法,可以快速的进行数据查询操作,查询条件可以用于读取、更新和删除等操作,主要涉及到where方法等连贯相关方法操作即可, 此框架查询系统可以解决不同数据库的差异性,因些我们把框架的这一查询方式称之为查询语言。使其查询操作更加简单易懂。 二、查询方式: ThinkPHP可以支持直接使用字符串作为查询条...

[PHP]curl上传多文件

码一下curl上传多文件的行 5.5之前版本的写法 $file = array( 'pic[0]'=>"@E:\wwwroot\10003\temp_56.ini;type=text/html;filename=temp_56.ini", 'pic[1]'=>"@E:\wwwroot\10003\...

HashMap在并发下可能出现的问题分析

我们都知道,HashMap在并发环境下使用可能出现问题,但是具体表现,以及为什么出现并发问题,可能并不是所有人都了解,这篇文章记录一下HashMap在多线程环境下可能出现的问题以及如何避免。 在分析HashMap的并发问题前,先简单了解HashMap的put和get基本操作是如何实现的。 1.HashMap的put和get操作 大家知道HashMap内部实...