[list]
[*][b]生成连续的一列数字或字母:[/b]
my @numbers = (1..100);
my @chars = (a..z);
[*][b]添加或者删除元素[/b]
shift: 移除数组的第一个元素;
unshift: 添加一个元素到数组的最后;
push: 添加一个元素到数组的第一个元素;
pop:去除数组的最后一个元素。
[table]
|Functio|Definition|
|push(@array, Element)| Adds to the end of an array|
|pop(@array)|Removes the last element of the array|
|unshift(@array, Element)|Adds to the beginning of an array|
|shift(@array)| Removes the first element of an array|
|delete $array[index]| Removes an element by index number|
[/table]
[*] [b]截取数组片断[/b]
my @a=(1..10);
my @b=@a[1..4, 8..10]; # @b = (2,3,4,5,9,10)
my @c=@a[1,9]; # @c = (2,10)
[*] [b]添加删除元素
[/b]
splice(@array, offset, length, $elem)
my @a=(1..10);
splice(@a, 1, 0, 100) # @a=(1,100,2,3,4,5,6,7,8,9,10)
my @b=(1..10);
splice(@b, 1, 1, 99,100) # @b=(1,99,100,3,4,5,6,7,8,9,10)
[/list]
[*][b]生成连续的一列数字或字母:[/b]
my @numbers = (1..100);
my @chars = (a..z);
[*][b]添加或者删除元素[/b]
shift: 移除数组的第一个元素;
unshift: 添加一个元素到数组的最后;
push: 添加一个元素到数组的第一个元素;
pop:去除数组的最后一个元素。
[table]
|Functio|Definition|
|push(@array, Element)| Adds to the end of an array|
|pop(@array)|Removes the last element of the array|
|unshift(@array, Element)|Adds to the beginning of an array|
|shift(@array)| Removes the first element of an array|
|delete $array[index]| Removes an element by index number|
[/table]
[*] [b]截取数组片断[/b]
my @a=(1..10);
my @b=@a[1..4, 8..10]; # @b = (2,3,4,5,9,10)
my @c=@a[1,9]; # @c = (2,10)
[*] [b]添加删除元素
[/b]
splice(@array, offset, length, $elem)
my @a=(1..10);
splice(@a, 1, 0, 100) # @a=(1,100,2,3,4,5,6,7,8,9,10)
my @b=(1..10);
splice(@b, 1, 1, 99,100) # @b=(1,99,100,3,4,5,6,7,8,9,10)
[/list]
Perl数组操作技巧
本文介绍Perl中数组的基本操作,包括生成连续的数字或字母序列、添加或删除元素的方法及使用splice进行复杂的数据操作。
54

被折叠的 条评论
为什么被折叠?



