3.1.14 pushing onto and popping off an Array
We can add an element to the end of an array by pushing it onto the array. The first argument push takes is the original list. The first argument can be followed by either a single scalar, or a list of items. If we have a scalar following the first argument, it is added to the end of the original list. If we have a list following the first argument, the elements of the second list are tacked onto the end of the first list, one by one. Thus, push makes the first argument behave like a stack data structure. The following program shows several examples of the use of push.
Program 3.9
#!/usr/bin/perl
#file push1.pl
use strict vars;
$" = ", ";
my (@friends, $friend);
my @friends99 = ('Justin OMalley', 'Timothy Valley', 'Brent Hays',
'Michael Saden');
my @friends98 = ('John Hart', 'Jeff Ellis', 'Brett Walters', 'Blake Escort');
my $friend97 = 'Tom Toybee';
my @oldFriends = ('Chad Blonding', 'Jeff Perich');
@friends = ("Hakan Kvarnstrom");
push @friends, 'Tommi Eriksson', 'Jonas Olsson';
push @friends, (@friends99, @friends98);
push @friends, $friend97, @oldFriends;
print "@friends\n";
This function prints the names of the friends in the order given below.
Hakan Kvarnstrom, Tommi Eriksson, Jonas Olsson, Justin O'Malley, Timothy Valley, Brent Hays, Michael Saden, John Hart, Jeff Ellis, Brett Walters, Blake Escort, Tom Toybee, Chad Blonding, Jeff Perich
Although we have broken up the list into several lines, it is actually printed on one line.
The push function returns the length of the resulting list. In the following program, the value returned by push is used to assign a variable $newLength.
Program 3.10
#!/usr/bin/perl
use strict vars;
$" = ", ";
my (@list1, @list2);
@list1 = ('Jeff Perich', 'Justin OMalley', 'Aaron Sturtevent');
@list2 = ('Michael Saden', 'Brett Walters', 'John Hart', 'Matt Van Zandt');
my $newLength = push @list1, @list2;
print "newLength = $newLength\n";
The value of $newLength at the end is 7. At the end of the program, $list1 has 7 items and $list2 has the original 4 items.
In Perl, most functions return a value. This is a characteristic of functional programming that Perl supports. The thing to note here is that push does not return the new list, but its length. This is unlike some other programming languages that support functional programming, such as LISP. Therefore, it would be wrong to write
my @list1 = push @list1, @list2;
if we want @list1 to contain the new list. It happens automatically without an assignment. In the following program, we perform such a wrong assignment.
Program 3.11
#!/usr/bin/perl
use strict vars;
$" = ", ";
my (@list1, @list2);
@list1 = ('Jeff Perich', 'Justin OMalley', 'Aaron Sturtevent');
@list2 = ('Michael Saden', 'Brett Walters', 'John Hart',
'Matt Van Zandt');
@list1 = push @list1, @list2; #WRONG! WRONG! WRONG!
print "list1 = @list1\n";
At the end of the program, @list1 does not contain the seven names, but it becomes an one-element list containing the number 7Ñthe length of the the resulting list after the push. It is because
push @list1, @list2; #WRONG! WRONG! WRONG!
returns the number 7. On the left hand side of the assignment
@list1 = push @list1, @list2;
we have the list @list1 and on the right hand side, the scalar 7. Perl coerces 7 into a list containing one element 7.
The function pop takes a list as an argument and removes the last element of the list. The value returned by pop is the element removed from the end of the list. The side effect of the pop operation is that the original list is reduced by one element.
In the following program, we pop items one by one from a list. The popping of individual elements is done inside a while loop.
Program 3.12
#!/usr/bin/perl
use strict vars;
my $friend;
my @friends = ('Justin OMalley', 'Timothy Valley', 'Brent Hays',
'Michael Saden',
'Matt Van Zandt', 'Reeves Smith', 'Matt Bolton');
#An array in a scalar context such as the condition of a while loop
while (@friends){
$friend = pop (@friends);
print "$friend\n";
}
At the end of the program, the list @friends has nothing in it. It is because all elements in it have been popped. We use @friends as the condition of the while loop. The conditional of a while loop expects a scalar value which it treats as a boolean. In a scalar context, a list is evaluated as the number of elements in it. Inside the while loop, we pop an element off the right of the loop. In every iteration, the list is reduced by one element. Therefore, when the last element has been popped, the conditional returns a value of 0. 0 is considered false in Perl and the while loop gets finished. Following our discussions earlier, the while loop can also be written as the following.
while (scalar (@friends)){
$friend = pop (@friends);
print "$friend\n";
}
An alternative way to push an element is the following.
@friends = (@friends, $newFriend);
Here, $newFriend is tacked onto the end of the list @friends and the resulting list is called @friends. This is the same as doing push except that the number of elements in the resulting list is not returned.
