在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Perl的数组操作有四大常用函数: push:从数组的末尾加入元素。 shift: 从数组的开头取出元素
1、push #!/usr/bin/perl my @array = (); for ( my $i = 1 ; $i <= 5 ; ++$i ) { output: 1
#!/usr/bin/perl use strict; my @array = ( 1, 2, 3, 4, 5, 6 ); while (@array) { output: 1 2 3 4 5
3、shift #!/usr/bin/perl use strict; my @array = ( 1, 2, 3, 4, 5, 6 ); while (@array) { output: 2 3 4 5 6
4、unshift #!/usr/bin/perl use strict; my @array = (); for ( my $i = 1; $i <= 5; ++$i ) { output: 1
另外,perl的数组还有其它重要函数,如splice、subtr、split、join、sort等: 5、splice 操作数组中间部分的函数: 5.1、向数组中间插入内容 #!/usr/bin/perl use strict; my @array = ( 0 .. 6 ); my @replaced = splice( @array, 3, 2, @array1 ); print "replaced: @replaced\n", output: replaced: 3 4
#!/usr/bin/perl use strict; my @array = ( 0 .. 6 ); my @replaced = splice( @array, 3, 2 ); print "replaced: @replaced\n", output: replaced: 3 4
#!/usr/bin/perl use strict; my @array = ( 0 .. 6 ); my @replaced = splice( @array, 3 ); print "replaced: @replaced\n", output: replaced: 3 4 5 6 #!/usr/bin/perl use strict; my @array = ( 0 .. 6 ); print "$replaced\n", output: 0 perl -le '$p=q(/var/ftp/test);@a=split(/\/ftp\//,$p);print $a[1];'
8、scalar #!/usr/bin/perl use strict; my @array = ( 0 .. 6 ); print "$count1\n"; output: 7
9、sort #!/usr/bin/perl use strict; my @array = ( 0 .. 9 ); # create an unsorted array of numbers and sort it
Original: 0 1 2 3 4 5 6 7 8 9 Unsorted: 100 23 9 75 5 10 2 50 7 96 1 40
|
请发表评论