A Perl array is a list variable that stores scalar values. The variables can be of different types.
Array variables start with @. To access array elements, use the $ + variable name + [index value] format to read. The example is as follows:
In the program, the $ symbol is escaped with , allowing it to be output as is.
$hits[0] = 25$hits[1] = 30$hits[2] = 40$names[0] = google$names[1] = codercto$names[2] = taobao
Array variables start 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/This is an array/;
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/googletaobao...codercto/;
You can also assign values to an array by index, as follows:
$array[0] = 'Monday';...$array[6] = 'Sunday';
To access array elements, use the $ + variable name + [index value] format to read. The example is as follows:
googletaobaocoderctocodercto
Array index values start from 0, that is, 0 is the first element, 1 is the second element, and so on.
Negative numbers are read from the reverse direction, -1 is the first element, -2 is the second element
Perl provides an array format that can be output in sequence. The format is starting value + .. + end value . The example is as follows:
1 2 3 4 5 6 7 8 9 1010 11 12 13 14 15 16 17 18 19 20a bcdefghijklmnopqrstuv wxyz
The array size is determined by the scalar context within the array. :
@array = (1,2,3);print "array size: ",scalar @array,"n";
The array length returns the physical size of the array, not the number of elements. We can see the following example:
Array size: 51 Maximum index: 50
As can be seen from the output, there are only four array elements, but the array size is 51.
Perl provides some useful functions for adding and removing 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 commonly used operation functions in arrays:
serial number | Type and description |
---|---|
1 | push @ARRAY, LIST Put the value of the list at the end of the array |
2 | pop @ARRAY Pop the last value of the array and return it |
3 | shift @ARRAY Pops the first value of the array and returns it. The index value of the array is also decremented by one. |
4 | unshift @ARRAY, LIST Places the list in front of the array and returns the number of elements in the new array. |
We can cut an array and return the new array after cutting:
weibo qq facebook
Array index needs to specify a valid index value, which can be a positive number followed by a negative number. Each index value is separated by a comma.
If it is a continuous index, you can use .. to indicate the specified range:
weibo qq facebook
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: 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 an array starting from the 6th element:
Before replacement - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 After replacement - 1 2 3 4 5 21 22 23 24 25 11 12 13 14 15 16 17 18 19 20
To convert a string into an array in Perl, use the split() function. The syntax 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.
ocomweibo
In Perl, the join() function is used to convert an array into a string. The syntax is as follows:
join EXPR, LIST
Parameter description:
EXPR: Connector.
LIST: List or array.
www-codercto-comgoogle,taobao,codercto,weibo
Array sorting in Perl uses the sort() function, and the syntax is as follows:
sort [SUBROUTINE] LIST
Parameter description:
SUBROUTINE: Specify rules.
LIST: List or array.
Before sorting: google taobao codercto facebook After sorting: facebook google codercto 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.
The special variable $[ represents the first index value of the array, which is generally 0. If we set $[ to 1, the first index value of the array is 1, the second is 2, and so on. Examples are as follows:
Website: google taobao codercto facebook@sites[1]: google@sites[2]: taobao
In general, we do not recommend using the special variable $[ . In the new version of Perl, this variable has been abandoned.
The elements of the array are separated by commas. We can also use commas to merge arrays, as shown below:
numbers = 1 3 4 5 6
It is also possible to embed multiple arrays within an array and merge them into the main array:
numbers = 1 3 5 2 4 6
A list can be used as an array. Specifying the index value after the list can read the specified element, as shown below:
The value of var is = 1
Similarly, we can use .. in an array to read elements in a specified range:
value of list = 4 3 2