Section 3.4. List Assignment


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:[*]

[*] As opposed to languages like C, which has no easy way to do this in general. C programmers use an auxiliary swap variable to hold the value temporarily, possibly managed via a macro.

     ($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.[*]

[*] Well, that's true for scalar variables. Array variables get an empty list as you'll see in a moment.

     ($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:[]

] Were cheating by assuming that the rocks array is empty before this statement. If there were a value in $rocks[7], say, this assignment wouldn't affect that element.

     ($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."[] This works on either side of the assignment operator:

] Larry claims that he chose the dollar and at sign because they can be read as $calar (scalar) and @rray (array). If you dont get that or remember it that way, no big deal.

     @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.

[§] But in the Alpaca book, we'll show you a special kind of scalar called a reference. That lets us make what are informally called "lists of lists" among other interesting and useful structures. In that case, you're still not storing a list into a list; you're storing a reference to an array.

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.

[*] Of course, we're joking, but there's a kernel of truth in this joke. Indexing into arrays is not using Perl's strengths. If you use the pop, push, and similar operators that avoid using indexing, your code will generally be faster than if you use many indices, as well as be more likely to avoid "off-by-one" errors, often called "fencepost" errors. Occasionally, a beginning Perl programmer (wanting to see how Perl's speed compares to C's) will take, say, a sorting algorithm optimized for C (with many array index operations), rewrite it straightforwardly in Perl (again, with many index operations), and wonder why it's so slow. The answer is that using a Stradivarius violin to pound nails should not be considered a sound construction technique.

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.



Learning Perl
Learning Perl, 5th Edition
ISBN: 0596520107
EAN: 2147483647
Year: 2003
Pages: 232

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net