Perl 数组应用详解(push, pop, shift, unshift)

本文深入解析Perl编程语言中数组操作的四大常用函数:push、pop、shift和unshift,并通过实例代码展示其用法和效果。同时,文章还介绍了其他重要函数如splice、join、split和sort,以及如何使用它们来高效地处理数组。通过具体代码示例,读者可以快速掌握Perl数组操作技巧。

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


### JavaScript 数组 `pop`、`push`、`unshift`、`shift` 方法详解 #### 1. `pop()` 方法 `pop()` 方法用于移除数组中的最后一个元素,并返回被移除的元素。此方法会直接修改原始数组,将其长度减一。 如果数组为空,则返回 `undefined`[^2]。 示例代码如下: ```javascript const arr = ['JS', 'Array', 'pop']; const removedElement = arr.pop(); console.log(removedElement); // 输出 'pop' console.log(arr); // 输出 ["JS", "Array"] ``` --- #### 2. `push()` 方法 `push()` 方法用于向数组的末尾添加一个或多个元素,并返回新数组的长度。同样,此方法也会直接修改原始数组。 示例代码如下: ```javascript let arr = ['pop', 'push']; const newLength = arr.push('shift', 'unshift'); console.log(newLength); // 输出 4 console.log(arr); // 输出 ["pop", "push", "shift", "unshift"] ``` --- #### 3. `unshift()` 方法 `unshift()` 方法用于在数组的开头添加一个或多个元素,并返回新数组的长度。它会对原数组进行修改,在其头部插入指定的元素。 需要注意的是,当传入的参数是一个数组时,整个数组会被作为一个单独的元素加入到目标数组中[^5]。 示例代码如下: ```javascript const arr = ['Array', 'shift']; const newLength = arr.unshift('JS'); console.log(newLength); // 输出 3 console.log(arr); // 输出 ["JS", "Array", "shift"] // 当参数为数组时 const newArr = []; newArr.unshift([1, 2]); console.log(newArr); // 输出 [[1, 2]] ``` --- #### 4. `shift()` 方法 `shift()` 方法用于移除数组的第一个元素,并返回该元素。此方法会改变原数组的内容,使后续元素向前移动一位填补空缺位置。 对于空数组调用 `shift()` 将返回 `undefined`[^3]。 示例代码如下: ```javascript const arr = ['JS', 'Array', 'shift']; const firstElement = arr.shift(); console.log(firstElement); // 输出 'JS' console.log(arr); // 输出 ['Array', 'shift'] ``` --- ### 总结对比表 | **方法** | **作用** | **是否修改原数组** | **返回值** | |-------------|--------------------------------------------|--------------------|--------------------------| | `pop()` | 移除数组最后的一个元素 | 是 | 被移除的元素 | | `push()` | 向数组末尾追加一个或多个元素 | 是 | 添加后的数组长度 | | `unshift()` | 在数组开头新增一个或多个元素 | 是 | 插入后的数组长度 | | `shift()` | 删除数组第一个元素 | 是 | 被删除的首个元素 | 这些方法都属于对数组的操作类函数,能够快速调整数组内容而无需手动遍历重写数据结构[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值