ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
3.4 Array Operators and FunctionsArray functions and operators act on entire arrays. Some return a list, which can then either be used as a value for another array function, or assigned into an array variable. 3.4.1 AssignmentProbably the most important array operator is the array assignment operator, which gives an array variable a value. It is an equal sign, just like the scalar assignment operator. Perl determines whether the assignment is a scalar assignment or an array assignment by noticing whether the assignment is to a scalar or an array variable. For example: @fred = (1,2,3); # The fred array gets a three-element literal @barney = @fred; # now that is copied to @barney If a scalar value is assigned to an array variable, the scalar value becomes the single element of an array: @huh = 1; # 1 is promoted to the list (1) automatically Array variable names may appear in a list literal list. When the value of the list is computed, Perl replaces the names with the current values of the array, like so: @fred = qw(one two); @barney = (4,5,@fred,6,7); # @barney becomes # (4,5,"one","two",6,7) @barney = (8,@barney); # puts 8 in front of @barney @barney = (@barney,"last");# and a "last" at the end # @barney is now (8,4,5,"one","two",6,7,"last") Note that the inserted array elements are at the same level as the rest of the literal: a list cannot contain another list as an element.[2]
If a list literal contains only variable references (not expressions), the list literal can also be treated as a variable. In other words, such a list literal can be used on the left side of an assignment. Each scalar variable in the list literal takes on the corresponding value from the list on the right side of the assignment. For example: ($a,$b,$c) = (1,2,3); # give 1 to $a, 2 to $b, 3 to $c ($a,$b) = ($b,$a); # swap $a and $b ($d,@fred) = ($a,$b,$c); # give $a to $d, and ($b,$c) to @fred ($e,@fred) = @fred; # remove first element of @fred to $e # this makes @fred = ($c) and $e = $b If the number of elements being assigned does not match the number of variables to hold the values, any excess values (on the right side of the equal sign) are silently discarded, and any excess variables (on the left side of the equal sign) are given the value of An array variable appearing in the array literal list must be last, because the array variable is "greedy" and consumes all remaining values. (Well, you could put other variables after it, but they would just get If an array variable is assigned to a scalar variable, the number assigned is the length of the array, as in: @fred = (4,5,6); # initialize @fred $a = @fred; # $a gets 3, the current length of @fred The length is also returned whenever an array variable name is used where a scalar value is needed. (In the upcoming section called "Scalar and List Context," we'll see that this is called using the array name in a scalar context.) For example, to get one less than the length of the array, you can use $a = @fred; # $a gets the length of @fred ($a) = @fred; # $a gets the first element of @fred The first assignment is a scalar assignment, and so The value of an array assignment is itself a list value, and can be cascaded as you can with scalar assignments. For example: @fred = (@barney = (2,3,4)); # @fred and @barney get (2,3,4) @fred = @barney = (2,3,4); # same thing 3.4.2 Array Element AccessSo far, we've been treating the array as a whole, adding and removing values by doing array assignments. Many useful programs are constructed using arrays without ever accessing any specific array element. However, Perl provides a traditional subscripting function to access an array element by numeric index. For the subscripting function, array elements are numbered using sequential integers, beginning at zero[3] and increasing by one for each element. The first element of the @fred = (7,8,9); $b = $fred[0]; # give 7 to $b (first element of @fred) $fred[0] = 5; # now @fred = (5,8,9)
Other elements can be accessed with equal ease, as in: $c = $fred[1]; # give 8 to $c $fred[2]++; # increment the third element of @fred $fred[1] += 4; # add 4 to the second element ($fred[0],$fred[1]) = ($fred[1],$fred[0]); # swap the first two Accessing a list of elements from the same array (as in that last example) is called a slice, and occurs often enough that there is a special representation for it: @fred[0,1]; # same as ($fred[0],$fred[1]) @fred[0,1] = @fred[1,0]; # swap the first two elements @fred[0,1,2] = @fred[1,1,1];# make all 3 elements like the 2nd @fred[1,2] = (9,10); # change the last two values to 9 and 10 Note that this slice uses an Slices also work on literal lists, or any function that returns a list value: @who = (qw(fred barney betty wilma))[2,3]; # like @x = qw(fred barney betty wilma); @who = @x[2,3]; The index values in these examples have been literal integers, but the index can also be any expression that returns a number, which is then used to select the appropriate element: @fred = (7,8,9); $a = 2; $b = $fred[$a]; # like $fred[2], or the value of 9 $c = $fred[$a-1]; # $c gets $fred[1], or 8 ($c) = (7,8,9)[$a-1]; # same thing using slice Perl programs can thus have array accesses similar to many traditional programming languages. This idea of using an expression for the subscript also works for slices. Remember, however, that the subscript for a slice is a list of values, so the expression is an array expression, rather than a scalar expression. @fred = (7,8,9); # as in previous example @barney = (2,1,0); @backfred = @fred[@barney]; # same as @fred[2,1,0], or ($fred[2],$fred[1],$fred[0]), or # (9,8,7) If you access an array element beyond the end of the current array (that is, an index of greater than the last element's index), the @fred = (1,2,3); $barney = $fred[7]; # $barney is now undef Assigning a value beyond the end of the current array automatically extends the array (giving a value of @fred = (1,2,3); $fred[3] = "hi"; # @fred is now (1,2,3,"hi") $fred[6] = "ho"; # @fred is now (1,2,3,"hi",undef,undef,"ho") You can use A negative subscript on an array counts back from the end. So, another way to get at the last element is with the subscript -1. The second to the last element would be -2, and so on. For example: @fred = ("fred", "wilma", "pebbles", "dino"); print $fred[-1]; # prints "dino" print $#fred; # prints 3 print $fred[$#fred]; # prints "dino" 3.4.3 The push and pop FunctionsOne common use of an array is as a stack of information, where new values are added to and removed from the right-hand side of the list. These operations occur often enough to have their own special functions: push(@mylist,$newvalue); # like @mylist = (@mylist,$newvalue) $oldvalue = pop(@mylist); # removes the last element of @mylist The The @mylist = (1,2,3); push(@mylist,4,5,6); # @mylist = (1,2,3,4,5,6) Note that the first argument must be an array variable name; pushing and popping wouldn't make sense on a literal list. 3.4.4 The shift and unshift FunctionsThe unshift(@fred,$a); # like @fred = ($a,@fred); unshift(@fred,$a,$b,$c); # like @fred = ($a,$b,$c,@fred); $x = shift(@fred); # like ($x,@fred) = @fred; # with some real values @fred = (5,6,7); unshift(@fred,2,3,4); # @fred is now (2,3,4,5,6,7) $x = shift(@fred); # $x gets 2, @fred is now (3,4,5,6,7) As with 3.4.5 The reverse FunctionThe @a = (7,8,9); @b = reverse(@a); # gives @b the value of (9,8,7) @b = reverse(7,8,9); # same thing Note that the argument list is unaltered; the @b = reverse(@b); # give @b the reverse of itself 3.4.6 The sort FunctionThe @x = sort("small","medium","large"); # @x gets "large","medium","small" @y = (1,2,4,8,16,32,64); @y = sort(@y); # @y gets 1,16,2,32,4,64,8 Note that sorting numbers does not happen numerically, but by the string values of each number ( 3.4.7 The chomp FunctionThe @stuff = ("hello\n","world\n","happy days"); chomp(@stuff); # @stuff is now ("hello","world","happy days") |