Perl的标量是单数
列表和数组为复数
列表(list): 标量的有序集合,指的是数据
数组(array): 存储列表的变量,指的是变量
列表的值不一定放在数组里,每个数组变量一定包含一个列表
列表/数组中的每个元素都是一个单独的标量变量,拥有独立的标量值,这些值是有序的,每个元素都有相应的整数作为索引,此数字从0开始递增
1. 访问数组中的元素
$fred[0] = 'yabba';
$fred[1] = 'dabba';
$fred[2] = 'doo';
数组的名字空间和标量的名字空间是完全分开的,数组fred与标量fred是不同的东西
print $fred[0];
$fred[2] = 'diddley';
$fred[1] .= 'whatsis';
任何求值能得到数字的表达式都可以作为下标,假如它不是整数,则会自动舍去小数,无论正负
$number = 2.71828;
print $fred[$number - 1]; #结果和 print $fred[1]相同
超出索引则为undef
$blank = $fred[ 142_857 ]; # 未使用的数组元素,会得到undef的结果
$blanc = $mel; # 未使用的标量 $mel,也会得到undef的结果
2. 特殊的数组索引
对索引值超过数组尾端的元素进行赋值,数组会自动扩大,增补元素的默认取值是undef
$rocks[0] = 'bedrock';
$rocks[1] = 'slate';
$rocks[2] = 'lava';
$rocks[3] = 'crushed rock';
$rocks[99] = 'schist'; # 现在有95个undef元素
最后一个元素的索引值 $#rocks,这个数字比数组元素个数小1,因为数组索引是从0开始的
$end = $#rocks;
$number_of_rocks = $end + 1;
$rocks[$#rocks] = 'hard rock';
从数组尾端往回计数的“负数数组索引值”,不过超出数组大小的负数索引值是不会绕回去的,一般只使用-1这个索引值
3. 列表直接量
列表直接量:在程序代码中表示一列数据的写法
(1, 2, 3)
(1, 2, 3,) # 末尾的逗号会被忽视
('fred', 4.5)
() # 空列表,0个元素
范围操作符…,用于自动创建它两侧标量值间的所有取值
(1..100) # 从 1到100的整数序列
(1..5) # (1,2,3,4,5)
(1.7..5.7) # 小数点部分去掉后取范围
(0, 2..6, 10, 12) # (0, 2, 3, 4, 5, 6, 10, 12)
范围操作符只能从小到大依次累加
(5..1) # 空列表
列表中的元素可以是表达式
($m, 17)
($m+$o, $p+$q)
($m..$n)
(0..$#rocks)
4. qw简写
快速输入,避免反复输入引号和逗号
qw( fred barney betty wilma dino)
qw表示"quoted word"/“quoted by whitespace”
在qw构建的列表中,不能像双引号内的字符串一样使用\n或$fred,其中的空白字符(空格、制表符、换行符等)会被舍弃
qw(
fred
barney betty
wilma dino
) # 和上面创建的列表一样
不能将注释放在qw列表中
允许使用任何标点符号作为定界符
qw! fred barney betty wilma dino !
qw/ fred barney betty wilma dino /
qw# fred barney betty wilma dino #
qw( fred barney betty wilam dino)
qw{ fred barney betty wilam dino }
qw[ fred barney betty wilam dino ]
qw< fred barney betty wilam dino >
如果需要在被圈引得字符串中使用定界符,通过反斜线转义来引入这个字符
qw ! yahoo\! google ask msn ! # 将 yahoo! 作为1个元素引入
2个连续得反斜线表示1个实际的反斜线
5. 列表的赋值
($fred, $barney, $dino) = ('flintstone', 'rubble', undef);
列表是在赋值运算前建立的,可以利用这个特性轻松的交换2个变量的值
($fred, $barney) = ($barney, $fred); # 交换这2个变量的值
($betty[0], $betty[1]) = ($betty[1], betty[0]);
变量的个数多于给定的列表值个数,多出来的变量被定义为undef(或者空列表)
给定的列表值个数多于变量值,多出来的列表值会被舍弃
($fred, $barney) = qw[flintstone];
($fred, $barney) = q< filtstone rubble slate granite>;
构建1个字符串数组
($rocks[0], $rocks[1], $rocks[2], $rocks[3]) = qw/talc mica feldspar quartz/;
使用@(all of the)来引用整个数组
@rocks = qw/ bedrock slate lava/;
@tiny = (); # 空列表
@giant = 1..1e5; # 包含 100 000 个元素的列表
@stuff = (@giant, undef, @giant); # 包含 200 001 个元素的列表
$dino = "granite";
@quarry = (@rocks, "crushed rock", @tiny, $dino); # (bedrock, slate, lava, crushed rock, granite)
@tiny贡献了0个元素,数组名会被展开成它所拥有的元素列表!!!数组只能包含标量,不能包含其它数组
引用: 列表的列表,列表中每个元素都是另一个列表的引用
将列表赋值给另一个列表,仍是列表的赋值运算
@copy = @quarry; # 将1个数组中的列表复制到另1个数组中
6. pop和push操作符
要新增元素到数组尾端,只需把它放到具有更高索引的位置
数组常用作信息堆栈(stack),常对数组右边即尾端添加新元素或者删除旧元素
pop操作符用于提取数组末端的元素并将此作为返回值
@array = 5..9;
$fred = pop(@array); # $fred 为9, @array 现在是(5, 6, 7, 8)
$barney = pop @array; # $barney 为8, @array 现在是(5, 6, 7)
pop @array; # @array 现在是(5, 6) , 7被舍弃了
空上下文: 表示返回值无处可去
pop的常见用法,删除数组中的最后1个元素
如果数组是空的,pop什么都不做,直接返回undef
push添加1个/1串元素到数组的末端
push(@array, 0);
push @array, 8;
push @array, 1..10;
@others = qw/ 9 0 2 1 0 /;
push @array, @others;
7. shift和unshift操作符
push和pop操作符处理的是数组的尾端(数组的最右边)
shift和unshift操作符处理的是数组的开头(数组的最左边)
@array = qw# dino fred barney #;
$m = shift(@array);
$n = shift @array;
shift @array;
$o = shift @array;
unshift(@array, 5);
unshift @array, 4;
@others = 1..3;
unshift @array, @others;
与pop相似,对于一个空数组,shift会返回undef
8. splice操作符
添加或移除中间的某些元素
splice最多可接受4个参数,最后2个是可选参数
- 操作的目标数组
- 要操作的元素的开始位置
- 要操作的元素个数
- 要替代的列表
@array = qw( pebbles dino fred barney betty );
@removed = splice @array,2; # 从数组中删除fred往后的元素, @removed为qw( fred barney betty ), @array变成qw( pebbles dino )
@array = qw( pebbles dino fred barney betty );
@removed = splice @array, 1, 2; # 删除dino和fred这2个元素, @array变成qw( pebbles barney betty )
@array = qw( pebbles dino fred barney betty );
@removed = splice @array, 1, 2, qw(wilma); # 删除dino和fred这2个元素, @remove变成qw( dino fred ), @array变成qw( pebbles wilma barney betty )
@array = qw( pebbles dino fred barney betty );
@removed = splice @array, 1, 0, qw(wilma); # 什么元素都不删, @removed变成qw(), @array变成qw( pebbles wilma dino fred berney betty )
先插入新元素,再删除旧元素