Latest web development tutorials

Perl Array

An array is a scalar value stored unordered list of variables.

@ The beginning of an array of variables. Access array elements using$ variable + name + [index] to read format, examples are as follows:

#!/usr/bin/perl

@hits = (25, 30, 40);             
@names = ("google", "w3big", "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 program uses the $ symbol \ to escape, so he is output.

The above program, the output is:

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

Creating an array

Array variable with the@ symbol start, the element is in brackets can also be qwbegin defining arrays.

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

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

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

You can also assign an index to the array, as follows:

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

Access array elements

Access array elements using$ variable + name + [index] to read format, examples are as follows:

@sites = qw/google taobao w3big/;

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

The above program, the output is:

google
taobao
w3big
w3big

Array index starts from 0, which is 0 for the first element, the second element is 1, and so on.

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


Array serial number

Perl provides a sequence can be in the form of an array of output in the format ofthe start value end value + + .., examples are 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

Context is determined by the size of the array array scalar. :

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

Returns the length of the array is an array of physical size, rather than the number of elements, we can see the following examples:

#!/uer/bin/perl

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

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

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

The above program, the output is:

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

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


Adding and removing array elements

Perl provides a number of useful functions to add and remove array elements.

If no programming experience before you may ask what is the function, in fact, before weprint use, that is an output function.

The following table lists the common array manipulation functions:

No. Type and Description
1 push @ARRAY, LIST

The list of values ​​into the end of the array

2 pop @ARRAY

Pop the last value array and returns it

3 shift @ARRAY

Array pop the first value and returns it. The index value of the array are sequentially minus one.

4 unshift @ARRAY, LIST

The list in front of an array and returns the number of elements in the new array.

Examples

#!/usr/bin/perl

# 创建一个简单是数组
@sites = ("google","w3big","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 program, the output is:


Cutting array

We can cut an array, and returns a new array after cutting:

#!/usr/bin/perl

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

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

print "@sites2\n";

The above program, the output is:

weibo qq facebook

You need to specify a valid index of the array index value can be negative after a positive number, each index value with commas.

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

#!/usr/bin/perl

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

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

print "@sites2\n";

The above program, the output is:

weibo qq facebook

Replace array elements

Perl array elements are used interchangeably in the splice () function syntax is as follows:

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

Parameter Description:

  • @ARRAY: To replace the array.
  • OFFSET: starting position.
  • LENGTH: Replace the number of elements.
  • LIST: Replace the element list.

The following examples are beginning to replace the sixth element of the array of five elements:

#!/usr/bin/perl

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

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

The above program, 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

Converts string into an array

Perl will convert a string array using the split () function syntax is as follows:

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

Parameter Description:

  • PATTERN: separator, default spaces.
  • EXPR: Specifies the number of strings.
  • LIMIT: If you specify this parameter, the number of elements in the array returned.

Examples

#!/usr/bin/perl

# 定义字符串
$var_test = "w3big";
$var_string = "www-w3big-com";
$var_names = "google,taobao,w3big,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

The above program, the output is:

o
com
weibo

The array is converted to a string

Perl in the array to a string using the join () function syntax is as follows:

join EXPR, LIST

Parameter Description:

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

Examples

#!/usr/bin/perl

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

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


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

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

The above program, the output is:

www-w3big-com
google,taobao,w3big,weibo

Sorting an array

Perl sort arrays using sort () function syntax is as follows:

sort [ SUBROUTINE ] LIST

Parameter Description:

  • SUBROUTINE: specify rules.
  • LIMIT: list or array.

Examples

#!/usr/bin/perl

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

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

The above program, the output is:

排序前: google taobao w3big facebook
排序前: facebook google w3big taobao

Note: Sorting an array is sorted according to the ASCII numeric value. So we had better first converted when each element of the array sort lowercase before sorting.


Special variables: $ [

The special variable$ [a first index of the array, generally 0, if we $ [setto 1, the array's first index value is 1, the second is 2, and so on. Examples are as follows:

#!/usr/bin/perl

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

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

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

The above program, the output is:

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

Generally we do not recommend using the special variable$ [in the new version of Perl, the variable is obsolete.


Merge array

Element of the array is a comma to separate, we can also use commas to merge array, as follows:

#!/usr/bin/perl

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

print "numbers = @numbers\n";

The above program, the output is:

numbers = 1 3 4 5 6

You can also embed multiple arrays in an array, and merged into the main array:

#!/usr/bin/perl

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

@numbers = (@odd, @even);

print "numbers = @numbers\n";

The above program, the output is:

numbers = 1 3 5 2 4 6

Select the element from the list

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

#!/usr/bin/perl

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

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

The above program, the output is:

var 的值为 = 1

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

#!/usr/bin/perl

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

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

The above program, the output is:

list 的值 = 4 3 2