Getting started...login
Getting started with Perl
author:php.cn  update time:2022-04-14 16:05:12

Perl array


Array A is an unordered list variable that stores scalar values.

Array variables start with @. To access array elements, use the $ + variable name + [index value] format to read. The example is as follows:

#!/usr/bin/perl

@hits = (25, 30, 40);             
@names = ("google", "php", "taobao");

print "$hits[0] = $hits[0]\n";
print "$hits[1] = $hits[1]\n";
print "$hits[2] = $hits[2]\n";
print "$names[0] = $names[0]\n";
print "$names[1] = $names[1]\n";
print "$names[2] = $names[2]\n";

The $ symbol in the program is escaped with \, allowing it to be output as is.

Execute the above program, the output result is:

$hits[0] = 25
$hits[1] = 30
$hits[2] = 40
$names[0] = google
$names[1] = php
$names[2] = taobao

Create array

The array variable starts with the @ symbol, and the elements are placed in brackets , you can also define an array starting with qw.

@array = (1, 2, 'Hello');
@array = qw/这是 一个 数组/;

The second array uses the qw// operator, which returns a list of strings with array elements separated by spaces. Of course, you can also use multiple lines to define an array:

@days = qw/google
taobao
...
php/;

You can also assign values ​​to the array by index, as shown below:

$array[0] = 'Monday';
...
$array[6] = 'Sunday';

Accessing array elements

To access array elements, use the $ + variable name + [index value] format to read. The example is as follows:

@sites = qw/google taobao php/;

print "$sites[0]\n";
print "$sites[1]\n";
print "$sites[2]\n";
print "$sites[-1]\n";    # 负数,反向读取

Execute the above program, and the output result is:

google
taobao
php
php

array Index values ​​start at 0, which is 0 for the first element, 1 for the second element, and so on.

Negative numbers are read from the reverse direction, -1 is the first element, -2 is the second element


Array sequence number

Perl provides Array form that can be output in sequence, the format is start value + .. + end value, the example is as follows:

#!/usr/bin/perl

@var_10 = (1..10);
@var_20 = (10..20);
@var_abc = (a..z);

print "@var_10\n";   # 输出 1 到 10
print "@var_20\n";   # 输出 10 到 20
print "@var_abc\n";  # 输出 a 到 z
<p>执行以上程序,输出结果为:</p>
<pre>
1 2 3 4 5 6 7 8 9 10
10 11 12 13 14 15 16 17 18 19 20
a b c d e f g h i j k l m n o p q r s t u v w x y z

Array size

The array size is given by Determined by the context of the scalar in the array. :

@array = (1,2,3);
print "数组大小: ",标量 @array,"\n";

The array length returns the physical size of the array, not the number of elements. We can see the following example:

#!/uer/bin/perl

@array = (1,2,3);
$array[50] = 4;

$size = @array;
$max_index = $#array;

print "数组大小:  $size\n";
print "最大索引: $max_index\n";

Execute the above program, the output result is:

数组大小:  51
最大索引: 50

As can be seen from the output results, there are only four array elements, but the array size is 51.


Add and remove array elements

Perl provides some useful functions to add and remove array elements.

If you have no programming experience before, you may ask what a function is. In fact, the print we used before is an output function.

The following table lists the commonly used operation functions in arrays:

Serial numberType and description
1push @ARRAY, LIST

Push the value of the list to the end of the array

2pop @ARRAY

Pop the last value of the array and return it

3 shift @ARRAY

Pop the first value of the array and return it. The index value of the array is also decremented by one.

4unshift @ARRAY, LIST

Put the list in front of the array and return the elements of the new array number.

Example

#!/usr/bin/perl

# 创建一个简单是数组
@sites = ("google","php","taobao");
print "1. \@sites  = @sites\n";

# 在数组结尾添加一个元素
push(@sites, "baidu");
print "2. \@sites  = @sites\n";

# 在数组开头添加一个元素
unshift(@sites, "weibo");
print "3. \@sites  = @sites\n";

# 删除数组末尾的元素
pop(@sites);
print "4. \@sites  = @sites\n";

# 移除数组开头的元素
shift(@sites);
print "5. \@sites  = @sites\n";

Execute the above program, the output result is:


##Cut array

We can cut an array , and return the new array after cutting:

#!/usr/bin/perl

@sites = qw/google taobao php weibo qq facebook 网易/;

@sites2 = @sites[3,4,5];

print "@sites2\n";

Execute the above program, the output result is:

weibo qq facebook

The array index needs to specify a valid index value, which can be a positive number followed by a negative number, each index value Separate with commas.

If it is a continuous index, you can use

.. to indicate the specified range:

#!/usr/bin/perl

@sites = qw/google taobao php weibo qq facebook 网易/;

@sites2 = @sites[3..5];

print "@sites2\n";

Execute the above program, the output result is:

weibo qq facebook


Replace array elements

The splice() function is used to replace array elements in Perl. The syntax format is as follows:

splice @ARRAY, OFFSET [ , LENGTH [ , LIST ] ]

Parameter description:

  • @ARRAY : The array to be replaced.

  • OFFSET: Starting position.

  • LENGTH: The number of elements to replace.

  • LIST: List of replacement elements.

The following example replaces 5 elements in the array starting from the 6th element:

#!/usr/bin/perl

@nums = (1..20);
print "替换前 - @nums\n";

splice(@nums, 5, 5, 21..25); 
print "替换后 - @nums\n";

Execute the above program, the output result is:

替换前 - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
替换后 - 1 2 3 4 5 21 22 23 24 25 11 12 13 14 15 16 17 18 19 20


Convert a string to an array

In Perl, use the split() function to convert a string to an array. The syntax format is as follows:

split [ PATTERN [ , EXPR [ , LIMIT ] ] ]

Parameter description:

  • PATTERN: delimiter, default is space.

  • EXPR: Specify the number of strings.

  • LIMIT: If this parameter is specified, the number of elements in the array is returned.

Example

#!/usr/bin/perl

# 定义字符串
$var_test = "php";
$var_string = "www-php-com";
$var_names = "google,taobao,php,weibo";

# 字符串转为数组
@test = split('', $var_test);
@string = split('-', $var_string);
@names  = split(',', $var_names);

print "$test[3]\n";  # 输出 com
print "$string[2]\n";  # 输出 com
print "$names[3]\n";   # 输出 weibo

Execute the above program, the output result is:

o
com
weibo


Convert the array to a string

In Perl, the join() function is used to convert an array into a string. The syntax format is as follows:

join EXPR, LIST

Parameter description:

  • EXPR: connector.

  • LIST: List or array.

Example

#!/usr/bin/perl

# 定义字符串
$var_string = "www-php-com";
$var_names = "google,taobao,php,weibo";

# 字符串转为数组
@string = split('-', $var_string);
@names  = split(',', $var_names);


# 数组转为字符串
$string1 = join( '-', @string );
$string2 = join( ',', @names );

print "$string1\n";
print "$string2\n";

Execute the above program, the output result is:

www-php-com
google,taobao,php,weibo


Array sorting

Array sorting in Perl Use the sort() function, the syntax format is as follows:

sort [ SUBROUTINE ] LIST

Parameter description:

  • SUBROUTINE: Specify the rule.

  • LIMIT: list or array.

Example

#!/usr/bin/perl

# 定义数组
@sites = qw(google taobao php facebook);
print "排序前: @sites\n";

# 对数组进行排序
@sites = sort(@sites);
print "排序前: @sites\n";

Execute the above program, the output result is:

排序前: google taobao php facebook
排序前: facebook google php taobao

Note: Array sorting is based on ASCII numeric values. So when we sort the array, it is best to convert each element to lowercase before sorting.


Special variables: $[

Special variables

$[ represents the first index value of the array, which is generally 0. If we $[ is set to 1, then the first index value of the array is 1, the second is 2, and so on. The example is as follows:

#!/usr/bin/perl

# 定义数组
@sites = qw(google taobao php facebook);
print "网站: @sites\n";

# 设置数组的第一个索引为 1
$[ = 1;

print "\@sites[1]: $sites[1]\n";
print "\@sites[2]: $sites[2]\n";

Execute the above program, the output result is:

网站: google taobao php facebook
@sites[1]: google
@sites[2]: taobao

Generally, we do not recommend the use of special variables

$[. In the new version of Perl, This variable is obsolete.


Merge arrays

The elements of the array are separated by commas. We can also use commas to merge arrays, as shown below:

#!/usr/bin/perl

@numbers = (1,3,(4,5,6));

print "numbers = @numbers\n";

Execute the above program, the output result is:

numbers = 1 3 4 5 6

You can also embed multiple arrays in the array and merge them into the main array:

#!/usr/bin/perl

@odd = (1,3,5);
@even = (2, 4, 6);

@numbers = (@odd, @even);

print "numbers = @numbers\n";

Execute the above program, the output result is: :

numbers = 1 3 5 2 4 6

Select elements from the list

A list can be used as an array, and the specified element can be read by specifying the index value after the list, as follows:

#!/usr/bin/perl

$var = (5,4,3,2,1)[4];

print "var 的值为 = $var\n"

Execute the above program, the output result is:

var 的值为 = 1

Similarly, we can use .. in the array to read the elements in the specified range:

#!/usr/bin/perl

@list = (5,4,3,2,1)[1..3];

print "list 的值 = @list\n";

Executing the above program, the output result is:

list 的值 = 4 3 2

php.cn