perl中数组的使用
来源:互联网 发布:websock java 编辑:程序博客网 时间:2024/05/01 01:40
Perl的数组操作有四大常用函数,分别是:
1、push:从数组的末尾加入元素
#!/usr/bin/perl
use strict;
use warnings;
my @array = ();
for ( my $i = 1 ; $i<= 5 ; ++$i ) {
}
#!/usr/bin/perl
use strict;
use warnings;
my @array = ( 1, 2, 3, 4, 5, 6);
while (@array) {
}
3、shift:
#!/usr/bin/perl
use strict;
use warnings;
my @array = ( 1, 2, 3, 4, 5, 6);
while (@array) {
}
#!/usr/bin/perl
use strict;
use warnings;
my @array = ();
for ( my $i = 1; $i<= 5; ++$i ) {
}
另外,perl的数组还有其它重要函数,如splice、subtr、split、join、sort等。
5、splice操作数组中间部分的函数,该函数主要有2个作用:
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:
#!/usr/bin/perl
use strict;
use warnings;
my @array
my @array1 = ( 'a' .. 'd' );
my @replaced = splice( @array, 3,2 );
print "replaced:
#!/usr/bin/perl
use strict;
use warnings;
my @array
my @array1 = ( 'a' .. 'd' );
my @replaced = splice( @array, 3);
print "replaced:
#!/usr/bin/perl
use strict;
use warnings;
my @array = ( 0 .. 6 );
my $replaced = join("\n", @array);
print "$replaced\n",
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";
9、sort:对数组元素进行排序
#!/usr/bin/perl
use strict;
use warnings;
my @array
my @reversed = reverse @array;
print "Original:
print "Reversed:
# create an unsorted array ofnumbers and sort it
my @array2
my @sortedLexically
my @sortedNumerically = sort { $a <=>$b } @array2;
print "Unsorted:
print "Lexically:
print "Numerically: @sortedNumerically\n"; // 1 2 5 7 9 10 23 40 50 75 96 100
- Perl中数组的使用
- perl中数组的使用
- perl中数组的定义和使用
- perl 数组的使用
- Perl函数参数中传递数组和引用的使用
- perl中数组的输出和scalar使用举例
- Perl函数参数中传递数组和引用的使用
- perl中使用的前缀
- perl中FTP的使用
- perl中chomp的使用
- Perl中say的使用
- perl中数组变量的初始化
- perl 中 数组 与 列表的 区别
- Perl中数组的delete操作
- perl函数中传递数组的方法
- perl中写数组的最值
- Perl去掉数组中重复的行
- perl-数组的用法
- Eclipse reports rendering library more recent than ADT plug-in.Please update ADT plug-in
- [开源工具]Visual Leak Detector 2.2.3 Visual C++内存检测工具
- 从零开始,重新学习windows编程(2)--环境准备,不用IDE编写程序
- 读曾仕强的《易经的奥秘》简摘
- eclipse编辑相关快捷键
- perl中数组的使用
- 关于制定计划
- 第三章 目标文件里有什么
- 如何查看80端口是否被占用_端口被占用解决办法
- web.xml中welcome-file配置的问题
- 【开源工具】Cppcheck 1.54 C/C++静态代码分析工具
- 从零开始,学习windows编程(3)—hello.c的疑惑
- HDU 2815 Mod Tree 解a^x = b(mod n)非互质
- 从零开始,学习windows编程(4)--从libc.lib开始