Perl 数组应用详解(push, pop, shift, unshift)
来源:互联网 发布:mac搜索应用程序 编辑:程序博客网 时间:2024/05/22 09:48
Perl的数组操作有四大常用函数:
push:从数组的末尾加入元素。
pop :从数组的末尾取出元素
shift:
unshift:从数组的开头加入元素
1、push
#!/usr/bin/perl
use strict;
use warnings;
my @array = ();
for ( my $i = 1 ; $i <= 5 ; ++$i ) {
}
output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
2、pop
#!/usr/bin/perl
####<pop>###
use strict;
use warnings;
my @array = ( 1, 2, 3, 4, 5, 6 );
while (@array) {
}
output:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
3、shift
#!/usr/bin/perl
####<shift>###
use strict;
use warnings;
my @array = ( 1, 2, 3, 4, 5, 6 );
while (@array) {
}
output:
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
4、unshift
#!/usr/bin/perl
####<unshift>###
use strict;
use warnings;
my @array = ();
for ( my $i = 1; $i <= 5; ++$i ) {
}
output:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
另外,perl的数组还有其它重要函数,如splice、subtr、split、join、sort等:
5、splice 操作数组中间部分的函数:
5.1、向数组中间插入内容
#!/usr/bin/perl
use strict;
use warnings;
my @array = ( 0 .. 6 );
my @array1 = ( 'a' .. 'd' );
my @replaced = splice( @array, 3, 2, @array1 );
print "replaced:
output:
replaced:
with:
resulting in: 0 1 2 a b c d 5 6
5.2、删除数组元素
#!/usr/bin/perl
use strict;
use warnings;
my @array
my @array1 = ( 'a' .. 'd' );
my @replaced = splice( @array, 3, 2 );
print "replaced:
output:
replaced:
with:
resulting in: 0 1 2 5 6
删除到末尾
#!/usr/bin/perl
use strict;
use warnings;
my @array
my @array1 = ( 'a' .. 'd' );
my @replaced = splice( @array, 3 );
print "replaced:
output:
replaced:
resulting in: 0 1 2
6、join 连接列表中的各个分离的串,生成一个新的串,返回一个标量!
#!/usr/bin/perl
use strict;
use warnings;
my @array = ( 0 .. 6 );
my $replaced = join("\n", @array);
print "$replaced\n",
output:
0
1
2
3
4
5
6
7、split
把字符串进行分割并把分割后的结果放入数组中
test
/var
8、scalar
统计数组的长度,一般我们不用这个,直接将数组赋值给标量即可。
#!/usr/bin/perl
use strict;
use warnings;
my @array = ( 0 .. 6 );
my $count1 = @array;
my $count2 = scalar @array;
print "$count1\n";
print "$count2\n";
output:
7
7
9、sort
对数组元素进行排序
#!/usr/bin/perl
use strict;
use warnings;
my @array
my @reversed = reverse @array;
print "Original:
print "Reversed:
# create an unsorted array of numbers and sort it
my @array2
my @sortedLexically
my @sortedNumerically = sort { $a <=> $b } @array2;
print "Unsorted:
print "Lexically:
print "Numerically: @sortedNumerically\n";
output:
Original:
Reversed:
Unsorted:
Lexically:
Numerically: 1 2 5 7 9 10 23 40 50 75 96 100
- Perl 数组应用详解(push, pop, shift, unshift)
- 操作Perl数组: shift, unshift, push, pop
- 操作Perl数组: shift, unshift, push, pop
- 【Perl读书笔记】数组操作push、pop、shift和unshift
- js 数组 pop,push,unshift,splice,shift
- 数组的操作push,pop,shift,unshift
- 数组添加删除----unshift,shift,pop,push
- pop,push,shift,unshift
- Perl学习(十三)——数组操作总结 push pop shift unshift
- shift/unshift/push/pop区别
- js中对于数组push(),pop(),shift(),unshift()方法的具体应用
- ActionScript中数组操作(一):push(pop)与unshift(shift)
- 数组的各种方法范例pop,push,unshift,splice,shift
- Js数组pop,push,unshift,splice,shift方法
- Js数组pop,push,unshift,splice,shift方法
- Js数组的方法pop,push,unshift,splice,shift
- Js数组的操作push,pop,shift,unshift等方法
- JavaScript数组函数unshift、shift、pop、push使用实例
- 挂载虚拟机镜像文件里的 LVM 逻辑分区
- dispatch与nsthread线程
- 为UIView及其子类添加圆角边
- 把DataTable中的身份证号导出excel的解决方案
- 【ios】iOS:KVO的概述与使用
- Perl 数组应用详解(push, pop, shift, unshift)
- 一位软件工程师的6年总结 .
- Cygwin上配置Hadoop的Java_home
- 简议IT规划
- Info.plist文件中重要的键
- ios抖动动画效果
- 笑话若干
- Marquee.js实现跑马灯效果
- 详喷TLD .