Summarize some of the uses of list and array operations in Perl
Suppose you define an array variable @arr and assign the list (str_a, Str_b, Str_c, str_d) to the array @arr
@arr = (str_a, Str_b, Str_c, str_d);
Assign the number of elements in an array @arr to $var
$var = @arr;
Print this array @arr, and the elements in the array are separated by spaces
print "@arr \ n";
Prints the first and third elements and the last element of the array @arr, with the default index starting at 0 and index-1 representing the last element
print "$arr [0], $arr [2], $arr [ -1]\n];
Prints the index value of the last element of an array @arr
Print "$ #arr \ n";
Pop operator, take out the last element of the array and assign it to $var
$var = Pop @arr;
Push operator, add an element to the end of the array
Push @arr, "str_e";
Shift operator to remove the first element of the array and assign it to $var
$var = Shift @arr;
Unshift operator, add an element to the beginning of the array
Unshift @arr, "str_f";
Splice operator,
If 2 parameters are specified, the first is an array and the second is an index, then all elements after this index of the array are fetched
@brr = Splice @arr, 2;
If you specify 3 arguments, the 3rd represents the number of elements to take out, such as the element that takes the array index 1 and a total of 2 elements that follow.
@brr = Splice @arr, 1, 2;
If you specify 4 arguments, the 4th represents the replacement of the previously fetched element, for example, Str_e replaces the 2 elements that are taken out
Splice @arr, 1,2, "str_e";
List bulk Assignment, qw a list of quotes that automatically enclose
($var _a, $var _b, $var _c) = QW (Apple pear banana);
The value of a variable in an interchange list
($var _a, $var _b) = ($var _b, $var _a);
Loops Print List elements, $_ represents the default variable in the list
foreach (1..10) {
print "$_\n";
}
Reverse operator, sort the elements in the list and assign them to another array
@rra = reverse (1..10);
Sort operator that sorts the elements in the list in ASCII order
@arr = sort qw (apple pear banana);
This article is from the "Strive for" blog, please be sure to keep this source http://carllai.blog.51cto.com/1664997/1174840
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/