perl-数组的用法
来源:互联网 发布:mac os 版本 编辑:程序博客网 时间:2024/04/27 16:51
push:从数组的末尾加入元素。
pop :从数组的末尾取出元素
shift: 从数组的开头取出元素
unshift:从数组的开头加入元素
例子如下:
1,push
#!/usr/bin/perl
####<push>###
use strict;
use warnings;
my @array = ();
for ( my $i = 1 ; $i <= 5 ; ++$i ) {
push @array, $i;
print "@array/n";
}
[root@drbd-2 crontab]# ./push.pl
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) {
my $firstTotal = pop(@array);
print "@array/n";
}
[root@drbd-2 crontab]# ./pop.pl
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) {
my $firstTotal = shift(@array);
print "@array/n";
}
[root@drbd-2 crontab]# ./shift.pl
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 ) {
unshift( @array, $i ); # add $i to front of @array
print "@array/n"; # display current @array
}
[root@drbd-2 crontab]# ./unshift.pl
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
5,splice
操作数组中间部分的函数,该函数主要有2个作用:
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: @replaced/n",
"with: @array1/n",
"resulting in: @array/n/n";
[root@drbd-2 crontab]# ./splice.pl
replaced: 3 4
with: a b c d
resulting in: 0 1 2 a b c d 5 6
2,删除数组元素
#!/usr/bin/perl
use strict;
use warnings;
my @array = ( 0 .. 6 );
my @array1 = ( 'a' .. 'd' );
my @replaced = splice( @array, 3, 2 );
print "replaced: @replaced/n",
"with: @array1/n",
"resulting in: @array/n/n";
[root@drbd-2 crontab]# ./splice.pl
replaced: 3 4
with: a b c d
resulting in: 0 1 2 5 6
删除到末尾
#!/usr/bin/perl
use strict;
use warnings;
my @array = ( 0 .. 6 );
my @array1 = ( 'a' .. 'd' );
my @replaced = splice( @array, 3 );
print "replaced: @replaced/n",
"resulting in: @array/n/n";
[root@drbd-2 crontab]# ./splice.pl
replaced: 3 4 5 6
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",
[root@drbd-2 crontab]# ./join.pl
0
1
2
3
4
5
6
7,split
把字符串进行分割并把分割后的结果放入数组中
[root@drbd-2 crontab]# perl -le '$p=q(/var/ftp/test);@a=split(///ftp///,$p);print $a[1];'
test
[root@drbd-2 crontab]# perl -le '$p=q(/var/ftp/test);@a=split(///ftp///,$p);print $a[0];'
/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";
[root@drbd-2 crontab]# ./join.pl
7
7
9,sort
对数组元素进行排序
#!/usr/bin/perl
use strict;
use warnings;
my @array = ( 0 .. 9 );
my @reversed = reverse @array;
print "Original: @array/n";
print "Reversed: @reversed/n/n";
# create an unsorted array of numbers and sort it
my @array2 = ( 100, 23, 9, 75, 5, 10, 2, 50, 7, 96, 1, 40 );
my @sortedLexically = sort @array2;
my @sortedNumerically = sort { $a <=> $b } @array2;
print "Unsorted: @array2/n";
print "Lexically: @sortedLexically/n";
print "Numerically: @sortedNumerically/n";
[root@drbd-2 crontab]# ./join.pl
Original: 0 1 2 3 4 5 6 7 8 9
Reversed: 9 8 7 6 5 4 3 2 1 0
Unsorted: 100 23 9 75 5 10 2 50 7 96 1 40
Lexically: 1 10 100 2 23 40 5 50 7 75 9 96
Numerically: 1 2 5 7 9 10 23 40 50 75 96 100
- perl-数组的用法
- perl-数组的用法
- perl 二维数组的用法实例
- Perl关联数组用法集锦
- Perl二维数组用法大全
- Perl中数组和哈希表的用法小结(ZZ)
- Perl数组的应用
- Perl数组的应用
- perl 数组的使用
- PERL的数组操作
- PERL数组的遍历
- perl-hash的用法
- [perl]defined的用法
- perl的特殊用法
- perl的select用法
- Perl入门(三)Perl的数组
- perl 命令行接受参数 ARGV数组用法
- perl二维数组的遍历
- reactos操作系统实现(31)
- 选择什么格式的磁盘分区才能让我的数据更安全一些呢?
- SUSE下安装.bin文件
- commons-logging和Log4j 日志管理
- 学新东西了
- perl-数组的用法
- BPM Server for Weblogic建立引擎的过程
- 把某个数变成8的倍数
- 开放式SOA有益于绩效管理追踪
- PERL打印收到的Socket消息
- c++ 内存 字节对齐
- 8.为Hello World程序编写JSP文件,Java文件和配置文件
- SQL Server中触发器实例讲解
- 传值调用&引用调用