3.4. List AssignmentIn much the same way as scalar values may be assigned to variables, list values may also be assigned to variables: ($fred, $barney, $dino) = ("flintstone", "rubble", undef); All three variables in the list on the left get new values, just as if we did three separate assignments. Since the list is built up before the assignment starts, this makes it easy to swap two variables' values in Perl:[72]
($fred, $barney) = ($barney, $fred); # swap those values ($betty[0], $betty[1]) = ($betty[1], $betty[0]); But what happens if the number of variables (on the left side of the equals sign) isn't the same as the number of values (from the right side)? In a list assignment, extra values are silently ignored -- Perl figures that if you wanted those values stored somewhere, you would have told it where to store them. Alternatively, if you have too many variables, the extras get the value undef.[73]
($fred, $barney) = qw< flintstone rubble slate granite >; # two ignored items ($wilma, $dino) = qw[flintstone]; # $dino gets undef Now that we can assign lists, you could build up an array of strings with a line of code like this:[74]
($rocks[0], $rocks[1], $rocks[2], $rocks[3]) = qw/talc mica feldspar quartz/; But when you wish to refer to an entire array, Perl has a simpler notation. Just use the at-sign (@) before the name of the array (and no index brackets after it) to refer to the entire array at once. You can read this as "all of the," so @rocks is "all of the rocks."[75] This works on either side of the assignment operator:
@rocks = qw/ bedrock slate lava /; @tiny = ( ); # the empty list @giant = 1..1e5; # a list with 100,000 elements @stuff = (@giant, undef, @giant); # a list with 200,001 elements $dino = "granite"; @quarry = (@rocks, "crushed rock", @tiny, $dino); That last assignment gives @quarry the five-element list (bedrock, slate, lava, crushed rock, granite), since @tiny contributes zero elements to the list. (In particular, it doesn't put an undef item into the list -- but we could do that explicitly, as we did with @stuff earlier.) It's also worth noting that an array name is replaced by the list it contains. An array doesn't become an element in the list, because these arrays can contain only scalars, not other arrays.[76]
The value of an array variable that has not yet been assigned is ( ), the empty list. Just as new, empty scalars start out with undef, new, empty arrays start out with the empty list. It's worth noting that when an array is copied to another array, it's still a list assignment. The lists are simply stored in arrays. For example: @copy = @quarry; # copy a list from one array to another 3.4.1. The pop and push OperatorsYou could add new items to the end of an array by simply storing them into elements with new, larger indices. But real Perl programmers don't use indices.[77] So in the next few sections, we'll present some ways to work with an array without using indices.
One 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. (This is the end with the "last" items in the array, the end with the highest index values.) These operations occur often enough to have their own special functions. The pop operator takes the last element off of an array, and returns it: @array = 5..9; $fred = pop(@array); # $fred gets 9, @array now has (5, 6, 7, 8) $barney = pop @array; # $barney gets 8, @array now has (5, 6, 7) pop @array; # @array now has (5, 6). (The 7 is discarded.) That last example uses pop "in a void context," which is merely a fancy way of saying the return value isn't going anywhere. There's nothing wrong with using pop in this way, if that's what you want. If the array is empty, pop will leave it alone (since there is no element to remove), and it will return undef. You may have noticed that pop may be used with or without parentheses. This is a general rule in Perl: as long as the meaning isn't changed by removing the parentheses, they're optional.[78]
The converse operation is push, which adds an element (or a list of elements) to the end of an array: push(@array, 0); # @array now has (5, 6, 0) push @array, 8; # @array now has (5, 6, 0, 8) push @array, 1..10; # @array now has those ten new elements @others = qw/ 9 0 2 1 0 /; push @array, @others; # @array now has those five new elements (19 total) Note that the first argument to push or the only argument for pop must be an array variable -- pushing and popping would not make sense on a literal list. 3.4.2. The shift and unshift OperatorsThe push and pop operators do things to the end of an array (or the right side of an array, or the portion with the highest subscripts, depending upon how you like to think of it). Similarly, the unshift and shift operators perform the corresponding actions on the "start" of the array (or the "left" side of an array, or the portion with the lowest subscripts). Here are a few examples: @array = qw# dino fred barney #; $a = shift(@array); # $a gets "dino", @array now has ("fred", "barney") $b = shift @array; # $b gets "fred", @array now has ("barney") shift @array; # @array is now empty $c = shift @array; # $c gets undef, @array is still empty unshift(@array, 5); # @array now has the one-element list (5) unshift @array, 4; # @array now has (4, 5); @others = 1..3; unshift @array, @others; # @array now has (1, 2, 3, 4, 5) Analogous to pop, shift returns undef if given an empty array variable. Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|