Perl array

Array One is a sequenceless table variable that stores the scale value.

Array variables begin with . The access array elements are read in the format of the variable name and index value, as follows:

#!/usr/bin/perl

@hits = (25, 30, 40);             
@names = ("google", "youj", "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";

In the program, the $ symbol uses the s to escape and let him output as is.

The above procedure is performed and the output is:

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

Create an array

Array variables start with @ of , elements are placed in parentheses, or arrays can be defined with qw.

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

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

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

You can also assign values to arrays by index, as follows:

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

Access array elements

The access array elements are read in the format of the variable name and index value, as follows:

@sites = qw/google taobao youj/;

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

The above procedure is performed and the output is:

google
taobao
youj
youj

The array index value starts at 0, i.e. 0 is the first element, 1 is the second element, and so on.

Negative numbers are read in reverse, -1 is the first element, and -2 is the second element


Array serial number

Perl provides an array of outputs that can be output in sequence in the form of a starting value of . . . . and an end value, 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

The size of the array

The size of the array is determined by the context of the calibration 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, and we can look at 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";

The above procedure is performed and the output is:

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

As can be seen from the output, 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 don't have programming experience before, you might ask what a function is, but the print we used earlier is an output function.

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

Serial number Type and description
1 push @ARRAY, LIST

Place the value of the list at the end of the array

2 pop @ARRAY

Pops up the last value of the array and returns it

3 shift @ARRAY

Pops up the first value of the array and returns it. The index value of the array is also reduced by one.

4 unshift @ARRAY, LIST

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

Instance

#!/usr/bin/perl

# 创建一个简单是数组
@sites = ("google","youj","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";

The above procedure is performed and the output is:

Perl array


Cut the array

We can cut an array and return a new array after the cut:

#!/usr/bin/perl

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

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

print "@sites2\n";

The above procedure is performed and the output is:

weibo qq facebook

Array indexes need to specify a valid index value, which can be negative after a positive number, separated by a comma for each index value.

If it is a continuous index, you can .. to represent the specified range:

#!/usr/bin/perl

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

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

print "@sites2\n";

The above procedure is performed and the output is:

weibo qq facebook

Replaces array elements

Array element substitution in Perl uses the splice() function in the following syntax format:

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

Description of the parameters:

  • @ARRAY: The array to replace.
  • OFFSET: Start position.
  • LENGTH: The number of elements replaced.
  • LIST: Replace the list of elements.

The following example replaces five elements in the array starting with the 6th element:

#!/usr/bin/perl

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

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

The above procedure is performed and the output 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 the string to an array

Converting strings to arrays in Perl uses the split() function in the following syntax format:

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

Description of the parameters:

  • PATTERN: Separator, space by default.
  • EXPR: Specifies the number of strings.
  • LIMIT: If this parameter is specified, the number of elements of the array is returned.

Instance

#!/usr/bin/perl

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

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

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

The above procedure is performed and the output is:

o
com
weibo

Convert the array to a string

In Perl, converting an array to a string uses the join() function, in the following syntax format:

join EXPR, LIST

Description of the parameters:

  • EXPR: Connector.
  • LIST: List or array.

Instance

#!/usr/bin/perl

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

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


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

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

The above procedure is performed and the output is:

www-youj-com
google,taobao,youj,weibo

Array sorting

Array sorting in Perl uses the sort() function in the syntax format as follows:

sort [ SUBROUTINE ] LIST

Description of the parameters:

  • SUBROUTINE: Specify the rule.
  • LIMIT: List or array.

Instance

#!/usr/bin/perl

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

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

The above procedure is performed and the output is:

排序前: google taobao youj facebook
排序前: facebook google youj taobao

Note: Array sorting is based on ASCII numeric values. So when we sort arrays, it's a good idea to convert each element to small case before sorting.


Special variable: $ . . .

The special $[ the first index value of the array, which is generally 0, $[ the first index value of the array is 1, the second is 2, and so on. Here's an example:

#!/usr/bin/perl

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

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

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

The above procedure is performed and the output is:

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

In general, we do not recommend using $[ which is discarded in the new version of Perl.


Merge the array

The elements of the array are split by commas, which we can also use to merge the arrays, as follows:

#!/usr/bin/perl

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

print "numbers = @numbers\n";

The above procedure is performed and the output is:

numbers = 1 3 4 5 6

You can also embed multiple arrays in an 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";

The above procedure is performed and the output is:

numbers = 1 3 5 2 4 6

Select the element from the list

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

#!/usr/bin/perl

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

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

The above procedure is performed and the output is:

var 的值为 = 1

Similarly, we can use .. in an array to read elements of a specified range:

#!/usr/bin/perl

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

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

The above procedure is performed and the output is:

list 的值 = 4 3 2