In Perl, all array variable names begin with @. For example:
my @list; # An array of numbers
You can declare and initialize the array in one step:
my @list = ("Sam", "Joe", "Fred", "Sue"); # An array of names
Perl uses zero-based indexing just like C, but with a slight twist. The elements of the array are not
@list[0], @list[1], @list[2], @list[3] # Not!
Instead, they are
$list[0], $list[1], $list[2], $list[3]
There is a good, if somewhat obscure, reason for this. Anything that begins with a dollar sign (“$”) results in a scalar. Anything that begins with an at sign (“@”) is an array. Finally, as you learn later, anything beginning with a percent sign (“%”) is something called a hash.
Because you are dealing with an array of strings (strings are scalars), you need to put the “$” in front of any element reference. As you’ll see in Chapter 6, “Hashes, References, and Complex Data Structures,” if you have an array of arrays, then you put a “@” in front of an element reference.
Scalars ($) and Arrays (@) have Different NamespacesThe scalar namespace is kept separate from the array namespace. Thus it is possible to have an array called @list and a scalar called $list. (In terms of style, this is really stupid.) It is possible to have code like the following: # Please don't name things like this my $foo = 1; # A scalar my @foo = (10, 20, 30); # An array print "Scalar is $foo "; print "First element of the array is $foo[0] "; However, it’s not good programming practice to name two variables by the same name even if they do differ by a prefix. Array elements can be used just like any ordinary scalar: my $average = ($list[0] + $list[1] + $list[2] + $list[3]) / 4; |
Finally, elements can even be printed using an ordinary print statement:
print "The first element is $list[0] ";
The special variable name $#array returns the index of the last element of the array. In other words, it returns the number of elements in the array minus 1.
For example, the following code prints all the elements of an array:
my @array = (1, 2, 3, 4); for (my $index = 0; $index <= $#array; $index++) { print "array[$index] = $array[$index] "; }
This program outputs the following:
array[0] = 1 array[1] = 2 array[2] = 3 array[3] = 4
Notice that the whole expression $array[$index] is interpreted even though it’s inside a string.
Common Mistake: Using C Style IterationC programmers use the following code to iterate over an array: // C Code #define ARRAY_SIZE 3 int array[ARRAY_SIZE] = {1, 3, 5}; // ... for (int i = 0; i < ARRAY_SIZE; i++) printf("array[%d] = %d ", i, array[i]); The key item in this example is the conditional (i < ARRAY_SIZE). Most C programmers are used to using the less than sign (<) in this context. This doesn’t work in Perl. Perl uses the less than or equal (<=) conditional. If you use the wrong conditional, you omit the last element: # We mistakenly use the C style conditional for (my $index = 0; $index < $#array; $index++) { print "array[$index] = $array[$index] "; } |
Listing 3.1 contains a program that finds the largest and smallest element in an array.
use strict; use warnings; # Array to examine my @array = (45, 98, 20, 12, 83, 64, 98, 33, 99); my $biggest; # Biggest element in the array my $smallest; # Smallest element in the array # Assume that the first element is the biggest / smallest $biggest = $array[0]; $smallest = $array[0]; # Loop through the rest of the elements of the array # checking for the biggest and smallest for (my $index = 1; $index <= $#array; $index++) { if ($array[$index] > $biggest) { $biggest = $array[$index]; } if ($array[$index] < $smallest) { $smallest = $array[$index]; } } print "Biggest is $biggest, smallest $smallest "; |
Elements of an array can be assigned and used just like ordinary scalars. But you can assign a value to the entire array at one time by using an array expression.
An array expression is just a list of scalars enclosed in parentheses. For example:
@array = (1, $data, "sam");
An array expression also can consist of a single array:
@array_dest = @array_source;
You can combine scalars with arrays. The following expression adds two elements ($prefix1 and $prefix2) to an array (@message) and assigns the resulting array to @full_message:
@full_message = ($prefix1, $prefix2, @message);
You can print arrays the same way that you print scalar variables. For example:
my @array = ("Tom", "Dick", "Harry"); print "The names are: @array. ";
This prints the following:
The names are: Tom Dick Harry.
Consider the following code:
my @array = (1); $array[5] = 4;
You know that $array[0] is 1 and that $array[5] is 4. But what about elements 1, 2, 3, and 4? Perl fills in the “hole” in the array with undefined values. So the elements of the @array are (1, undef, undef, undef, undef, 4).
A previous example initialized an array with a set of strings using the following statement:
my @array = ("Tom", "Dick", "Harry");
You can simplify this with the qw operator. (qw stands for “quote words.”) The same statement using this notation looks like
my @array = qw(Tom Dick Harry);