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
Always go with the choice that scares you the most, because that's the one that is going to require the most from you!
原文地址:https://www.cnblogs.com/sanquanfeng/p/2971686.html