数组 #

一、数组概述 #

数组是Perl中用于存储有序列表的数据结构。数组变量以 @ 开头。

1.1 创建数组 #

perl
my @empty = ();
my @numbers = (1, 2, 3, 4, 5);
my @strings = ("apple", "banana", "cherry");
my @mixed = (1, "two", 3.0, "four");

1.2 范围操作符 #

perl
my @range1 = (1..5);
my @range2 = (a..e);
my @range3 = (A..Z);

1.3 qw简写 #

perl
my @fruits = qw(apple banana cherry);

等价于:

perl
my @fruits = ("apple", "banana", "cherry");

二、访问数组元素 #

2.1 索引访问 #

索引从0开始,使用 $ 访问单个元素:

perl
my @colors = ("red", "green", "blue");

print $colors[0];
print $colors[1];
print $colors[2];

2.2 负索引 #

负索引从末尾开始:

perl
my @colors = ("red", "green", "blue");

print $colors[-1];
print $colors[-2];
print $colors[-3];

2.3 数组切片 #

perl
my @colors = ("red", "green", "blue", "yellow", "purple");

my @slice1 = @colors[0, 2];
my @slice2 = @colors[1..3];
my @slice3 = @colors[0, 2, 4];

三、数组操作 #

3.1 获取长度 #

perl
my @arr = (1, 2, 3, 4, 5);

my $length = scalar @arr;
my $length2 = @arr;
my $last_index = $#arr;

3.2 添加元素 #

末尾添加:

perl
my @arr = (1, 2, 3);
push @arr, 4;
push @arr, 5, 6;

开头添加:

perl
my @arr = (3, 4, 5);
unshift @arr, 1, 2;

3.3 删除元素 #

末尾删除:

perl
my @arr = (1, 2, 3, 4, 5);
my $last = pop @arr;

开头删除:

perl
my @arr = (1, 2, 3, 4, 5);
my $first = shift @arr;

3.4 任意位置操作 #

使用 splice 函数:

perl
my @arr = (1, 2, 3, 4, 5);

my @removed = splice @arr, 2, 1;
my @removed2 = splice @arr, 1, 2, ("new1", "new2");

四、遍历数组 #

4.1 foreach循环 #

perl
my @colors = ("red", "green", "blue");

foreach my $color (@colors) {
    print "$color\n";
}

4.2 默认变量$_ #

perl
my @colors = ("red", "green", "blue");

foreach (@colors) {
    print "$_\n";
}

4.3 for循环 #

perl
my @arr = (1, 2, 3, 4, 5);

for (my $i = 0; $i < @arr; $i++) {
    print "arr[$i] = $arr[$i]\n";
}

4.4 each函数(Perl 5.12+) #

perl
my @arr = ("a", "b", "c");

while (my ($index, $value) = each @arr) {
    print "$index: $value\n";
}

五、数组函数 #

5.1 排序 #

perl
my @arr = (3, 1, 4, 1, 5, 9, 2, 6);

my @sorted = sort @arr;
my @sorted_num = sort { $a <=> $b } @arr;
my @sorted_desc = sort { $b <=> $a } @arr;

字符串排序:

perl
my @words = qw(banana Apple cherry);
my @sorted = sort @words;
my @sorted_ci = sort { lc($a) cmp lc($b) } @words;

5.2 反转 #

perl
my @arr = (1, 2, 3, 4, 5);
my @reversed = reverse @arr;

5.3 过滤 #

perl
my @arr = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

my @evens = grep { $_ % 2 == 0 } @arr;
my @odds = grep { $_ % 2 == 1 } @arr;
my @big = grep { $_ > 5 } @arr;

5.4 映射 #

perl
my @arr = (1, 2, 3, 4, 5);

my @doubled = map { $_ * 2 } @arr;
my @squared = map { $_ ** 2 } @arr;
my @strings = map { "num_$_" } @arr;

5.5 查找 #

perl
my @arr = (1, 2, 3, 4, 5);

my $found = grep { $_ == 3 } @arr;
my $count = grep { $_ > 3 } @arr;

use List::Util qw(first);
my $first_match = first { $_ > 3 } @arr;

六、列表操作 #

6.1 列表赋值 #

perl
my ($a, $b, $c) = (1, 2, 3);
my ($first, @rest) = (1, 2, 3, 4, 5);
my ($x, $y, @z) = (1, 2);

6.2 交换值 #

perl
my ($a, $b) = (1, 2);
($a, $b) = ($b, $a);

6.3 列表展开 #

perl
my @arr1 = (1, 2, 3);
my @arr2 = (4, 5, 6);
my @combined = (@arr1, @arr2);

七、常用技巧 #

7.1 检查元素是否存在 #

perl
my @arr = (1, 2, 3, 4, 5);
my $target = 3;

my $found = grep { $_ == $target } @arr;
if ($found) {
    print "Found $target\n";
}

使用 List::Util

perl
use List::Util qw(any);

my @arr = (1, 2, 3, 4, 5);
if (any { $_ == 3 } @arr) {
    print "Found\n";
}

7.2 去重 #

perl
my @arr = (1, 2, 2, 3, 3, 3, 4);
my %seen;
my @unique = grep { !$seen{$_}++ } @arr;

7.3 求和与平均 #

perl
use List::Util qw(sum);

my @arr = (1, 2, 3, 4, 5);
my $total = sum @arr;
my $avg = $total / @arr;

7.4 最大最小值 #

perl
use List::Util qw(max min);

my @arr = (3, 1, 4, 1, 5, 9, 2, 6);
my $max = max @arr;
my $min = min @arr;

八、实践练习 #

练习1:数组统计 #

perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;
use List::Util qw(sum max min);

my @scores = (85, 92, 78, 90, 88, 95, 82);

my $total = sum @scores;
my $count = scalar @scores;
my $avg = $total / $count;
my $max_score = max @scores;
my $min_score = min @scores;

say "Scores: @scores";
say "Count: $count";
say "Sum: $total";
say "Average: " . sprintf("%.2f", $avg);
say "Max: $max_score";
say "Min: $min_score";

练习2:数组处理 #

perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

my @words = qw(Perl Python Ruby PHP JavaScript);

my @upper = map { uc } @words;
my @long = grep { length > 4 } @words;
my @sorted = sort @words;

say "Original: @words";
say "Upper: @upper";
say "Long (>4): @long";
say "Sorted: @sorted";

练习3:栈和队列 #

perl
#!/usr/bin/perl
use strict;
use warnings;
use v5.10;

my @stack;
push @stack, 1, 2, 3;
say "Stack: @stack";
say "Pop: " . pop @stack;
say "Stack: @stack";

my @queue;
push @queue, "a", "b", "c";
say "Queue: @queue";
say "Dequeue: " . shift @queue;
say "Queue: @queue";

九、总结 #

本章学习了:

  • 数组的创建和访问
  • 数组元素操作(push、pop、shift、unshift)
  • 数组遍历方法
  • 常用数组函数(sort、reverse、grep、map)
  • 列表操作技巧

下一章将学习哈希。

最后更新:2026-03-27