时间紧张,先记一笔,后续优化与完善。
1
数组的数组
1.1
创建和拜访二维数组
创建是最简略的,举例:
1:
@AoA = (["fruit","ball"],
2:
["home","bart","marge"],
3:
["george","jane","elory"]);
4:
print $AoA[2]->[1];
5:
#每一对相邻的大括号或中括号之间有一个隐含的->,所以也可以这样写
6:
print $AoA[2][1];
7:
8:
#整个列表被封装在小括号中,而不是中括号中,因为是给列表赋值,
9:
#而不是给引用赋值,如果要创建一个指向数组的引用,那么你要这样
10:
$ref_to_AoA =[ ["fred","barney","pebbles","bamm"],
11:
["homer","bart","marge","maggie",]
12:
];
13:
print $ref_to_AoA->[2][3];
14:
#注意这里有在中括号前面的箭头符号。因为这是引用,所以要在前面加上->
15:
16:
#还有一点就是可以利用负数索引从数组前面向前计数,
17:
$AoA[0][-2];
18:
#表示第一行的倒数第二个元素
1.2
自行成长
我们读入一个数据结构,是一个纯文本文件,每一行是一个结构的一行,并且没行 包括由空白分割的元素。
1:
while(<>)
2:
{
3:
chomp;
4:
@tmp = split;
5:
push @AoA,[@tmp];
6:
}
7:
#或者直接不必定名那个数组
8:
while(<>)
9:
{
10:
push @AoA, [split];
11:
}
12:
#如果想要引用指向一个数组的数组,可以这样
13:
while(<>)
14:
{
15:
push @ref_to_AoA,[split];
16:
}
17:
#如果要向数组中增加列,还是使用简略的赋值
18:
#如果向一个存在的行中附加一个新列
19:
push @{@AOA[0]},"wilma","betty";
20:
#注意push的参数必须以@开头
1.3
拜访和打印
1:
#单元素打印
2:
print $AoA[3][2];
3:
#遍历整个数组
4:
for $now (@AoA)
5:
{
6:
print "@$row\n";
7:
}
8:
#如果你想追踪脚标
9:
for $i (0..$#AoA)
10:
{
11:
print "row $i is:@{$AoA[$i]}\n";
12:
}
13:
#或者
14:
for $i (0..$#AoA)
15:
{
16:
for $j (0..$#{$AoA[$i]})
17:
{
18:
print "element $i $j is $AoA[$i][$j]\n";
19:
}
20:
}
21:
#为了简化上面的程序,我们可以使用临时变量,让事情变的简略。
22:
for $i (0..$#AoA)
23:
{
24:
$row = $AoA[$i];
25:
for $j (0..$@{$row})
26:
{
27:
print "element $i $j is $row->[$j]\n";
28:
}
29:
}
1.4
片段
如果要拜访一个多维数组的某一片段的元素,你可以用循环把变量一个个的取出来。
1:
@part = ();
2:
for ($y = 7 ;$y <13 ;$y++)
3:
{
4:
push @part,$AoA[4][$y];
5:
}
6:
#这个循环可以用一个数组片段来替代
7:
@part = @{$AoA[4]} [7..12];
8:
#如果要一个二维片段,同样也可以通过循环来获得
9:
#也可以用上面刚学到的方法
10:
for ($x=4;$x<=9;$x++)
11:
{
12:
push @new_AoA,[@{$AoA[$x]} [7..12]];
13:
}
14:
#如果你经常这么做,可以写一个子程序,或者到CPAN上下载一个
15:
#PDL(perl data language)模块
2
数组的散列
2.1
数组的散列的组成
1:
%HoA = ( flintstones =>["fred","barney"],
2:
jetsons =>["george","jane","elroy"],
3:
);
4:
#向散列增加另一个数组
5:
$HoA{teletubbies} = ["tinky","dispay","laa",];
2.2
生成数组的散列
从上面格式的文件中读取: flintsotnes: fred barney wilma dino
jetsons: george jane elroy
simpsons: homer marge bart
上面是填充数组的散列的技巧
1:
while(<>)
2:
{
3:
next unless s/^(.*?):\s*//;
4:
$HoA{$i}=[split];
5:
}
6:
#或者
7:
while($line = <>)
8:
{
9:
($who,$rest)=split/:\s*/,$line,2;
10:
@fields=split ' ',$rest;
11:
$HoA{$who}=[@fields];
12:
}
13:
#可以通过一下方法向已存在的数组追加新的成员
14:
push @{$HoA{flintstones}},"wilma","pebbles";
2.3
拜访和打印数组
打印全部的元素
1:
for $family (keys %HoA)
2:
{
3:
print "$family: @{$HoA{$family}}\n";
4:
}
5:
#你可以为数组增加索引
6:
for $family (keys %HoA)
7:
{
8:
print "$family: ";
9:
for $i (0..$#{$HoA{$family}})
10:
{
11:
print " $i = $HoA{$family}[$i]";
12:
}
13:
print "\n";
14:
}
15:
#或者通过以数组拥有的元素个数对他们排序
16:
for $family (sort {@{$HoA{$b}} <=> @{$HoA{$a}}} keys %HoA)
17:
{
18:
print "$family: @{$HoA{$family}}\n";
19:
}
20:
#也可以以元素的个数对数组排序,然后以元素的ASCII码次序停止排序
21:
for $family (sort {@{$HoA{$b}}<=>@{$HoA{$a}}} keys %HoA)
22:
{
23:
print "$family: ",join(",",sort @{$HoA{$family}}),"\n";
24:
}
3
散列的数组
3.1
散列的数组的组成。
1:
@AoH = ({husband => "barney",
2:
wife => "betty",
3:
son => "bamm",
4:
},
5:
{husband => "george",
6:
wife => "jane",
7:
son => "elroy",
8:
},
9:
{husband => "homer",
10:
wife => "marge",
11:
son => "bart",
12:
},
13:
);
14:
15:
16:
#向数组中添加另外一个散列,
17:
push @AoH,{husband => "fred",wife => "wilma",daughter=>"pebbles"};
3.2
生成散列的数组
从文件中读取上面的格式
husband=fred friend=barney
1:
while(<>)
2:
{
3:
$rec = {};
4:
for $field (split)
5:
{
6:
($key,$value) = split/=/,$field;
7:
$rec->{$key} = $value;
8:
}
9:
push @AoH,$rec;
10:
}
11:
12:
#或者用上面更简略的方法
13:
while(<>)
14:
{
15:
push @AoH,{split /[\s=]+/};
16:
}
17:
18:
#可以向上面这样向一个现存的散列添加新成员
19:
$AoH[0]{pet} = "dino";
20:
$AoH[2]{pet} = "santa's";
3.3
拜访和打印散列的数组
用上面的方法设置一个特定散列的键/值对
1:
$AoH[0]{husband} = "fred";
2:
#把第二个数组的丈夫变成大写
3:
$AoH[1]{husband} = ~s/(\w)/\u$1/;
4:
5:
#打印全部数据的方法:
6:
for $href (@AoH)
7:
{
8:
print "{ ";
9:
for $role (keys %$href)
10:
{
11:
print "$role=$href->{$role}";
12:
}
13:
print "}\n";
14:
}
15:
16:
#带着索引打印
17:
for $i (0..$#AoH)
18:
{
19:
print "$i is {";
20:
for $role (keys %{$AoH[$i]})
21:
{
22:
print "$role=$AoH[$i]{$role} ";
23:
}
24:
print "}\n";
25:
}
4
散列的散列
4.1
散列的散列的组成
1:
%HoH=(
2:
flintstones=>
3:
{
4:
husband => "fred",
5:
pal => "barney",
6:
},
7:
jetsons =>
8:
{
9:
husband => "george",
10:
wife => "jane",
11:
"his boy" => "elroy",
12:
},
13:
simpsons =>
14:
{
15:
husband => "homer",
16:
wife => "marge",
17:
kid => "bart",
18:
},
19:
);
20:
#向%HoH中增加一个匿名散列
21:
$HoH{mash} = {
22:
captain => "pierce",
23:
major => "burns",
24:
corporal=> "radar",
25:
}
4.2
生成散列的散列
从上面格式的文件中读取数据:
1:
flintstones: husband=fred pal=barney wife=wilma pet=dino
我们可以使用上面两个循环之一来完成任务
1:
while(<>)
2:
{
3:
next unless s/^(.*?):\S*//;
4:
$who = $1;
5:
for $field (split)
6:
{
7:
($key,$value)=split/=/,$field;
8:
$HoH{$who}{$key} = $value;
9:
}
10:
}
11:
12:
#或者
13:
while(<>)
14:
{
15:
next unless s/^(.*?):\S*//;
16:
$who = $1;
17:
$rec = {};
18:
$HoH{$who} = $rec;
19:
for $field (split)
20:
{
21:
($key,$value)= split/=/,$field;
22:
$rec->{$key}= $value;
23:
}
24:
}
4.3
拜访和打印散列的散列
1:
#设置键/值对
2:
$HOH{flintstones}{wife} = "wilma";
3:
4:
#要把某个键/值对变成大写,可对该元素应用替换
5:
$HoH{jetsons}{'his boy'} =~s/(\w)/\u$1/;
6:
7:
#打印全部的元素,利用前后遍历内外层散列键的方法
8:
for $family (keys %HoH)
9:
{
10:
print "$family: ";
11:
for $role (keys %{$HoH{$family}})
12:
{
13:
print "$role = $person ";
14:
}
15:
print "\n";
16:
}
17:
18:
#在非常大的散列中,可以用each同时把键和值都检索出来
19:
while (($family,$roles) = each %HoH)
20:
{
21:
print "$family: ";
22:
while (($role,$person) = each %$roles)
23:
{
24:
print "$role = $person";
25:
}
26:
print "\n";
27:
}
28:
29:
#排序输出
30:
for $family (sort keys %HoH)
31:
{
32:
print "$family: ";
33:
for $role (sort keys %{$HoH{$family}})
34:
{
35:
print "$role=$HoH{$family}{$role} ";
36:
}
37:
print "\n";
38:
}
5
函数的散列
6
以上内容摘自大骆驼,感谢
Author: GRC <grc@grc>
Date: 2013-05-31 19:22:49 CST
HTML generated by org-mode 6.33x in emacs 23
文章结束给大家分享下程序员的一些笑话语录: 古鸽是一种搜索隐禽,在中国快绝迹了…初步的研究表明,古鸽的离去,很可能导致另一种长着熊爪,酷似古鸽,却又习性不同的猛禽类——犤毒鸟
--------------------------------- 原创文章 By
列和数组
---------------------------------