列表,数组和哈希的嵌套
Perl的数据结构中最常用到的两种类型数组和哈希。
-
什么是数组(Perl语言入门(第五版)的解释)?
如果说Perl的标量代表的是单数(singular),那么在Perl里代表复数(plural)的就是列表和数组。
列表(list)指的是标量的有序集合,而数组(array)则是存储列表的变量。在Perl里,这两个术语常常混用。不过更精确地说,列表指的是数据,而数组指的是变量。列表的值不一定要放在数组里,但每个数组变量都一定包含一个列表(即使该列表可能是空的)。 -
什么是哈希(Perl语言入门(第五版)的解释)?
所谓的哈希就是一种数据结构,和数组的相同之处在于:可以容纳很多值(没有上限),并能随机存取。而区别在于:不像数组是以数字来检索,哈希是以名字来检索。也就是说检索用的键不是数字,而是保证唯一的字符串。
另一种看待哈希的方法,是将它想象成一大桶的数据,其中每个数据都有关联的标签。可以伸手到桶里任意取出一张标签,查看附着的数据是什么。但是桶里没有所谓的第一个元素,只有一堆数据。然而数组里,第一个元素为元素0.然后是元素1、2,等等。但是哈希里没有顺序,因此也没有第一,有的只是一些键/值对。这些键和值都是任意的标量,但是键总是会被转换成字符串。假如你以数字表达式50/20为键,那么它会被转换为一个含有三个字符的字符串"2.5"。
了解完了什么是数组、列表和哈希,下面我们开始分析它们是如何嵌套使用的:
Ⅰ. 列表嵌套列表(列表嵌套列表即为二维列表)
a. 列表的声明
方法一:
@list = (
["banana","apple","orange","pear"],
["cauliflower","lettuce","tomato","cucumber"],
["orange juice","apple juice","red tea","red wine"]
)
方法二:
@fruit = ("banana","apple","orange","pear")
@vegetable = ("cauliflower","lettuce","tomato","cucumber")
@drink = ("orange juice","apple juice","red tea","red wine")
正确方法:
@list = ([@fruit],[@vegetable],[@drink])
错误方法:
@list = (@fruit, @vegetable, @drink)
错误剖析:如果不用[]把每个子列表括起来,那么这个列表@list就相当于@fruit, @vegetable, @drink这三个列表拼接而成的一个一维列表相当于@list = ("banana","apple","orange","pear","cauliflower","lettuce","tomato","cucumber","orange juice","apple juice","red tea","red wine")
方法三:
正确方法:
list[0] = ("banana","apple","orange","pear")
错误剖析:这样的结果是$list[0] = "pear" 并且会报警告
b. 列表的访问
list[0] and @list[0] 两种表示都可以,但是 @list[0] 会报警告
Ⅱ. 列表嵌套哈希
a. 列表的声明
方法一:
@list = (
{
'fruit' => 'banana',
'vegetable' => 'cauliflower',
'drink' => 'orange juice'
},
{
'fruit' => 'apple',
'vegetable' => 'lettuce',
'drink' => 'apple juice'
},
{
'fruit' => 'orange',
'vegetable' => 'tomato',
'drink' => 'red tea'
},
{
'fruit' => 'pear',
'vegetable' => 'cucumber',
'drink' => 'red wine'
}
)
方法二:
%group1 = (
'fruit' => 'banana',
'vegetable' => 'cauliflower',
'drink' => 'orange juice'
)
%group2 = (
'fruit' => 'apple',
'vegetable' => 'lettuce',
'drink' => 'apple juice'
)
%group3 = (
'fruit' => 'orange',
'vegetable' => 'tomato',
'drink' => 'red tea'
)
%group4 = (
'fruit' => 'pear',
'vegetable' => 'cucumber',
'drink' => 'red wine'
)
正确方法:
@list = ({%group1}, {%group2}, {%group3}, {%group4})
错误方法:
@list = (%group1, %group2, %group3, %group4)
错误剖析:如果不用{}把每个子列表括起来,那么这个列表@list就相当于%group1, %group2, %group3, %group4 这四个哈希拼接而成的一个一维列表,相当于@list = ( 'fruit', 'banana', 'drink', 'orange juice', 'vegetable', 'cauliflower', 'fruit', 'apple', 'drink', 'apple juice', 'vegetable', 'lettuce', 'fruit', 'orange', 'drink', 'red tea', 'vegetable', 'tomato', 'fruit', 'pear', 'drink', 'red wine', 'vegetable', 'cucumber')
方法三:
正确方法:
list[0] = {%group1};
错误方法:
list[0] = 'orange juice' 并且有警告
list[0] = ['fruit','banana','vegetable','cauliflower','drink','orange juice']
b. 列表的访问
list[0]{fruit} are the same.
$list[0] = {'fruit' => 'banana','drink' => 'orange juice','vegetable' => 'cauliflower'}
Ⅲ. 哈希嵌套哈希
a. 哈希的声明
方法一:
my %hash = (
group1 => {
'fruit' => 'banana',
'vegetable' => 'cauliflower',
'drink' => 'orange juice'
},
group2 => {
fruit => 'apple',
vegetable => 'lettuce',
drink => 'apple juice'
},
group3 => {
'fruit' => 'orange',
'vegetable' => 'tomato',
'drink' => 'red tea'
},
group4 => {
fruit => 'pear',
vegetable => 'cucumber',
drink => 'red wine'
}
);
%hash = (
'group1', {'fruit', 'banana', 'drink', 'orange juice', 'vegetable', 'cauliflower'},
'group2', {'fruit', 'apple', 'drink', 'apple juice', 'vegetable', 'lettuce'},
'group3', {'fruit', 'orange', 'drink', 'red tea', 'vegetable', 'tomato'},
'group4', {'fruit', 'pear', 'drink', 'red wine', 'vegetable', 'cucumber'}
);
方法二:
%group1 = (
'fruit' => 'banana',
'vegetable' => 'cauliflower',
'drink' => 'orange juice'
)
%group2 = (
'fruit' => 'apple',
'vegetable' => 'lettuce',
'drink' => 'apple juice'
)
%group3 = (
'fruit' => 'orange',
'vegetable' => 'tomato',
'drink' => 'red tea'
)
%group4 = (
'fruit' => 'pear',
'vegetable' => 'cucumber',
'drink' => 'red wine'
)
正确方法:
%hash = (
'group1' => {%group1},
'group2' => {%group2},
'group3' => {%group3},
'group4' => {%group4}
);
错误方法:
%hash = (%group1, %group2, %group3, %group4);
错误剖析:这个哈希相当于%hash = %group4
%hash = (
'group1' => %group1,
'group2' => %group2,
'group3' => %group3,
'group4' => %group4
);
错误剖析:这个哈希的结果相当于 %hash = ('tomato','group4','orange juice','vegetable','cauliflower','group2','orange','drink','fruit','pear','group1','fruit','banana','drink','drink','red wine','vegetable','cucumber','group3','fruit','red tea','vegetable')
方法三:
hash{'group1'} = {
'fruit' => 'banana',
'vegetable' => 'cauliflower',
'drink' => 'orange juice'
};
hash{'group1'}{'fruit'} = 'banana' and list{group1) = {'fruit' => 'banana','drink' => 'orange juice','vegetable' => 'cauliflower'}
Ⅳ. 哈希嵌套列表
a. 哈希的声明
方法一:
%hash = (
'fruit' => ["banana","apple","orange","pear"],
'vegetable' => ["cauliflower","lettuce","tomato","cucumber"],
'drink' => ["orange juice","apple juice","red tea","red wine"]
);
%hash = (
'fruit', ["banana","apple","orange","pear"],
'vegetable', ["cauliflower","lettuce","tomato","cucumber"],
'drink', ["orange juice","apple juice","red tea","red wine"]
);
方法二:
@fruit = ("banana","apple","orange","pear")
@vegetable = ("cauliflower","lettuce","tomato","cucumber")
@drink = ("orange juice","apple juice","red tea","red wine")
%hash = (
'fruit', [@fruit],
'vegetable', [@vegetable],
'drink', [@vegetable]
);
方法三:
hash{'fruit'} = ["banana","apple","orange","pear"]
b. 哈希的访问
hash{'fruit'} = ['banana','apple','orange','pear']
结语:
好了!数组、列表和哈希的相互嵌套的分析到这里就要告一段落了。
由于本人才学疏浅,文章中有不对的或者表示不清楚的欢迎大家指正,谢谢!
网友评论