3.4. List Assignment
In much the same way as scalar values, list values may be assigned to variables:
($fred, $barney, $dino) = ("flintstone", "rubble", undef);
All three variables in the list on the left get new values, as if you 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:[*]
($fred, $barney) = ($barney, $fred); # swap those values ($betty, $betty) = ($betty, $betty);
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.[*]
($fred, $barney) = qw< flintstone rubble slate granite >; # two ignored items ($wilma, $dino) = qw[flintstone]; # $dino gets undef
Now that you can assign lists, you could build up an array of strings with a line of code like this:
($rocks, $rocks, $rocks, $rocks) = 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." 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 you 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.[§] 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.
When an array is copied to another array, it's still a list assignment. The lists are stored in arrays as in this example:
@copy = @quarry; # copy a list from one array to another
3.4.1. The pop and push Operators
You could add new items to the end of an array by storing them into elements with new, larger indices. But real Perl programmers don't use indices.[*] 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 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 leaves it alone (since there is no element to remove) and it returns 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. The converse operation is push, which adds an element (or a list of elements) to the end of an array:
] A reader from the educated class will recognize that this is a tautology.
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 10 new elements @others = qw/ 9 0 2 1 0 /; push @array, @others; # @array now has those five new elements (19 total)
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 Operators
The push and pop operators do things to the end of an array (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 (the "left" side of an array or the portion with the lowest subscripts). Here are a few examples:
@array = qw# dino fred barney #; $m = shift(@array); # $m gets "dino", @array now has ("fred", "barney") $n = shift @array; # $n gets "fred", @array now has ("barney") shift @array; # @array is now empty $o = shift @array; # $o 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.