Perl的数组操作有四大常用函数:

push:从数组的末尾加入元素。
pop :从数组的末尾取出元素

shift: 从数组的开头取出元素
unshift:从数组的开头加入元素

1、push

#!/usr/bin/perl
use strict;
use warnings;

my @array = ();

for ( my $i = 1 ; $i <= 5 ; ++$i ) {
    push @array, $i;
    print "@array\n";
}

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) {
    my $firstTotal = pop(@array);
    print "@array\n";
}

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) {
    my $firstTotal = shift(@array);
    print "@array\n";
}

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 ) {
   unshift( @array, $i );         # add $i to front of @array
   print "@array\n";              # display current @array
}

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:     @replaced\n",
      "with:         @array1\n",
      "resulting in: @array\n\n";

output:

replaced:     3 4
with:         a b c d
resulting in: 0 1 2 a b c d 5 6

5.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",
      "resulting in: @array\n\n";

output:

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";

output:

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",

output:

0
1
2
3
4
5
6

7、split
把字符串进行分割并把分割后的结果放入数组中

perl -le '$p=q(/var/ftp/test);@a=split(/\/ftp\//,$p);print $a[1];'
test
 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";

output:

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";

output:

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 数组应用详解(push, pop, shift, unshift)的更多相关文章

  1. js 的数组怎么push一个对象. Js数组的操作push,pop,shift,unshift JavaScrip

    push()函数用于向当前数组的添加一个或多个元素,并返回新的数组长度.新的元素将会依次添加到数组的末尾. 该函数属于Array对象,所有主流浏览器均支持该函数. 语法 array.push( ite ...

  2. 数组方法push() pop() shift() unshift() splice() sort() reverse() contact()浅拷贝 slice()原数组拷贝

    push() pop() shift() unshift() splice() sort() reverse() 参考资料:https://wangdoc.com/javascript/stdlib/ ...

  3. Vue push() pop() shift() unshift() splice() sort() reverse() ...

    Vue 变异方法 push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. pop() 方法用于删除并返回数组的最后一个元素. shift() 方法用于把数组的第一个元素从其中删除,并返回 ...

  4. js push(),pop(),shift(),unshift()

    以前没有太在意这些,这几天看<Javascript 设计模式与开发实践>(不得不说这是一本好书) 发现总是会用到这几个函数,可是有什么区别呢?? 简单来说是两套东西(数据结构时老师详细的讲 ...

  5. js数组方法push pop shift unshift的返回值

    push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. pop() 方法用于删除并返回数组的最后一个元素. unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度. s ...

  6. Js数组的操作push,pop,shift,unshift等方法详细介绍

    js中针对数组操作的方法还是比较多的,今天突然想到来总结一下,也算是温故而知新吧.不过不会针对每个方法进行讲解,我只是选择其中的一些来讲. 首 先来讲一下push和pop方法,这两个方法只会对数组从尾 ...

  7. js数组的操作push,pop,shift,unshift

    push(args)可以每次压入多个元素,并返回更新后的数组长度. var oldArr=[1,2,3]; alert(oldArr.push(4,[5,6]))–>5(这里只会将[5,6]当做 ...

  8. Js~数组的操作push,pop,shift,unshift

    说几个概念: 队列:先进先出堆栈:先进后出 shift:从集合中把第一个元素删除,返回这个元素的值pop:从集合中把最后一个元素删除,返回这个元素的值 unshift:在集合开头添加一个或者多个元素, ...

  9. js数组方法详解

    Array对象的方法-25个 /*js数组方法详解 */ /* * 1 concat() 用于连接多个数组或者值-------------- * 2 copyWithin() 方法用于从数组的指定位置 ...

随机推荐

  1. 【LeetCode 8】字符串转换整数 (atoi)

    题目链接 [题解] 注意越界的处理就好 简单题 还有.. 正的-2^31不能由2^31取相反数得到,因为正的int最多到2^31-1 [代码] class Solution { public: boo ...

  2. Android中的onWindowFocusChanged()方法详解

    Android中获取手机屏幕的高度和宽度,我们知道在onCreate方法中获取到的值都是为0的,有人说可以在onClick方法中获取值,这个也是个方法 ,但在onWindowFocusChanged方 ...

  3. XSS的原理分析与解剖:第三章(技巧篇)**************未看*****************

    ‍‍0×01 前言: 关于前两节url: 第一章:http://www.freebuf.com/articles/web/40520.html 第二章:http://www.freebuf.com/a ...

  4. unittest框架学习笔记二之discover

    coding=utf-8'''Created on 2018/3/29 author:star Project:discover测试用例''' import unittest,time,oslist= ...

  5. Java学习之DOS基础

    Dos命令行dir:列出当前目录下的文件和文件夹md :创建目录rd :删除目录cd :进入指定目录cd..:退回到上一级目录cd/:退回到根目录del:删除文件exit:退出dos命令行 进入dos ...

  6. 净心诀---python3生成器

    生成器的作用----减少程序运行的内存开销 生成器特点: 1.一个一个的取值,而不是一次性把所有数据创建出来,迭代器中的数据不取不创建2.只能按照顺序取,不能跳过也不能回头3.一个迭代器中的数据只能从 ...

  7. 54-Ubuntu-打包压缩-4-bzip2压缩和解压缩介绍

    bzip2 tar和bizp2命令结合可以实现文件打包和压缩 tar只负责打包,但不压缩 用bzip2压缩tar打包后的文件,其扩展名一般为xxx.tar.bz2 在tar命令有一个选项-j可以调用b ...

  8. Django添加生产环境配置

    在初始化阶段,Django会帮我们创建一个settings.py目录,所有Django的配置都在这个模块中,这样就会产生一些问题,比如同一份配置怎么来更好的区分开发环境和线上环境.当然可以在setti ...

  9. JS自运行函数的写法和MVVM框架数据驱动的底层逻辑

    1.JS自运行函数的写法 ( function(){ console.log(111)} )( ) !function(){ console.log(111) }() ( function(){}() ...

  10. CSIC_716_20191204【网络编程 OSI 七层结构】

     软件开发架构 C/S架构: Client: 客户端 Server: 服务端 比如: 微信客户端.QQ客户端等... 优点: - 软件的使用稳定 - 节省网络资源 缺点: - 安装麻烦,用户体验差 - ...