A list is a Perl data structure where the position of an item matters. A list can grow or shrink during program execution. Elements of a list are accessed by using numeric index. In a list, the first element is considered to be in position 0.
The following program declares five lists using my. It then assigns values to these lists. It changes the values of a few list elements. Finally, it prints corresponding elements of the list row by row.
Program 1.11
#!/usr/bin/perl
#file arrrays.pl
use strict;
my (@friends, @hometowns, @heights, @weights, @ages, $i);
@friends = qw(Tommy Chad Jeff Aaron Rick Sean Todd);
@hometowns = ('Washington', 'San Francisco', 'Boulder', 'Golden', 'Montreal',
'Montreal', 'Colo Springs');
@heights = (66, 73, 70, 68, 67, 67, 73);
@weights = (140, 180, 145, 155, 135, 140, 190);
@ages = (18, 23, 21, 23, 21, 21);
$weights [3] = 157;
$ages [6] = 22;
$hometowns [0] = 'Washington DC';
print "Name \t Hometown \t Height \t Weight \t Age \n";
print "-" x 65, "\n";
for ($i = 0; $i <= 6; $i++){
print
"$friends[$i] \t $hometowns[$i] \t $heights[$i] \t\t $weights[$i] \t\t $ages[$i]\n";
}
The five lists used by the program are @friends, @hometowns, @heights, @weights and @ages. In Perl, the name of every variable that stores list data must begin with the ÒatÓ sign (@). This is another of the unusual syntactic features of Perl. The lists @friends and @hometowns have string elements. The lists @heights, @weights and @ages have numbers.
One way to write a list is by writing out all of its elements separated by commas. For the time being, let us assume that a listÕs component elements are either all numbers or all strings. However, it is possible to have a list where some elements are numbers and some are strings.
Each list used in this program has several elements. For example, @hometowns has a list of single-quoted strings as its elements. Its 0th element is ÕWashingtonÕ and its 6th element is ÕColo SpringsÕ. In Perl, list elements are numbered starting 0. The 0th element of the list @hometowns is obtained by writing $hometowns[0]. When one accesses an element of a list, one must replace the @ in the name of the list by $. This is because the dollar sign ($) states that we are looking at a scalar, and a specific element of a list is always a scalar.
Let us now look at the first list @friends. It has seven strings as elements. We could have written the line that assigns a value to @friends in the regular manner.
@friends = ('Tommy', 'Chad', 'Jeff', 'Aaron', 'Rick', 'Sean', 'Todd');
Here, we have seven single-quoted strings as elements of the list. When such a situation arises, we can leave out the quotes and the commas, and write the list as we have done in the program.
@friends = qw(Tommy Chad Jeff Aaron Rick Sean Todd);
qw stands for quoted words. We can use qw only when the constituent elements are singly quoted strings. As discussed earlier, strings can be single- or double-quoted. Single-quoted strings do not allow us to use backslashed escape characters. Single-quoted strings also do not allow variable names to be used inside for interpolation or expansion. Double-quoted strings allow both of these.
Specific elements of a list can be assigned values during program execution.
$weights [3] = 157; $ages [6] = 22; $hometowns [0] = 'Washington DC';
Here, we change the value of $weights[3] and $hometown[0]. The array @ages initially is 6 elements long. It is made seven elements long by performing an assignment to $ages[6].
Perl allows assignment to the nth element of a list that is smaller than n elements long. Let the list be m elements long where m<n. In such a case, all the elements between index m and n-1 are assumed to have undef as the value. undef simply means that the value is undefined. For example, in the program above, @friends is 7 elements long. Its last index is 6. If we now make an assignment to the 19th element by writing
$friends [19] = 'Johnny';
elements $friends[7] through $friends[18] have undef as their value.
In the for loop at the bottom of the program, we access the $ith element of each of the lists and print the values separated by a tab. The output of the program is given below.
Name Hometown Height Weight Age ----------------------------------------------------------------- Tommy Washington DC 66 140 18 Chad San Francisco 73 180 23 Jeff Boulder 70 145 21 Aaron Golden 68 157 23 Rick Montreal 67 135 21 Sean Montreal 67 140 21 Todd Colo Springs 73 190 22
In a computer science curriculum, it is customary to have a class in the beginning of the curriculum that deals with data structures of various kinds and their implementations. In such a class, arrays and lists are discussed as two different multi-component of collection data structures. A data structure is an abstraction for an entity that can store data and has operations that can be performed on the data. The array data structure and the list data structures have different operations defined on them although it is possible to implement a list using an array.
The data structure we have discussed in this section is a list. A list is a collection of items accessible one after another, i.e., sequentially. At a minimum, a list can be accessed at the very beginning, and then can be traversed forwards (sometimes, backwards as well), one step at a time. Thus, the operations required on a list data structure at a minimum are obtaining the first element, and obtaining the next element given one element (and, obtaining the previous element given an element). A list data structure is not of fixed length, and can grow and shrink during the execution of the program.
An array is a data structure that also represents a collection of objects. All objects in an array have to be of the same type. An array can be thought of as a collection of items which are randomly accessible using numeric index. Thus, in the case of an array, an operation that is crucially needed is to obtain an element given its numeric index in the array. An array data structure is conceptually simpler than a list. An array is usually of fixed length. That is, there is a limit to the number of items that can be stored within an array, although this limit can be large. If an array is sufficiently large, it can be used to mimic a list which grows and shrinks within the limit imposed by the array’s size.
A list can be implemented in more than one way. Perl implements the list data structure using an array. Hence, in Perl, we use the two terms, list and array, interchangeably, although in a strict sense it is not correct to do so. Thus, conceptually although we have two data structures, in practice, we have just one.