If you think of a scalar as being a singular thing, arrays are the plural form. Just as you have a flock of sheep or a wunch of bankers, you can have an array of scalars.
An array is a list of (usually related) scalars all kept together. Arrays start with an @ (at sign), and are initialised thus:
my @fruit = ("apples", "oranges", "guavas", "passionfruit", "grapes"); my @magic_numbers = (23, 42, 69); my @random_scalars = ("mumble", 123.45, "willy the wombat", -300); |
As you can see, arrays can contain any kind of scalars. They can have just about any number of elements, too, without needing to know how many before you start. Really any number - tens or hundreds of thousands, if you've got the memory.
Arrays are discussed on page 6 of the Camel or by typing perldoc perldata.
So if we don't know how many items there are in an array, how can we find out? Well, there are a couple of ways.
First of all, Perl's arrays are indexed from zero. We can access individual elements of the array like this:
print $fruits[0]; # prints "apples" print $random_scalars[2]; # prints "willy the wombat" |
Wait a minute, why are we using dollar signs in the example above, instead of at signs? The reason is this: we only want a scalar back, so we show that we want a scalar. There's a useful way of thinking of this, which is explained in chapter 1 of the Camel: if scalars are the singular case, then the dollar sign is like the word "the" - "the name", "the meaning of life", etc. The @ sign on an array, or the % sign on a hash, is like saying "those" or "these" - "these fruit", "those magic numbers". However, when we only want one element of the array, we'll be saying things like "the first fruit" or "the last magic number" - hence the scalar-like dollar sign.
If we wanted what we call an array slice we could say:
@fruits[1,2,3]; # oranges, guavas, passionfruit @magic_numbers[0..1]; # 23, 42 |
You just learnt something new, by the way: the .. ("dot dot") range operator (see pages 90-91 of your Camel or perldoc perlop) which creates a temporary list of numbers between the two you specify - in this case 0 and 1, but it could have been 1 and 100 if we'd had an array big enough to use it on. You'll run into this operator again and again, so remember it.
Another thing you can do with arrays is insert them into a string, the same as for scalars:
print "My favourite fruits are @fruits\n"; # whole array print "Two types of fruit are @fruits[0,2]"; # array slice |
Returning to the point, how do we find the last element in an array? Well, there's a special variable called $#array which is the index of the last element, so you can say:
@fruit[0..$#fruit]; |
and you'll get the whole array. If you print $#fruit you'll find it's 4, which is not the same as the number of elements - 5. Remember that it's the index of the last element and that the index starts at zero, so you have to add one to it to find out how many elements in the array.
But wait! There's More Than One Way To Do It - and an easier way, at that. If you evaluate the array in a scalar context - that is, do something like this:
my $fruit_count = @fruits; |
... you'll get the number of elements in the array.
There's more than two ways to do it, as well - scalar(@fruits) and int(@fruits) will also tell us how many elements there are in the array.
There's a term you've heard used just recently but which hasn't been explained: context.
All Perl expressions are evaluated in a context. The two main contexts are:
scalar context, and
list context
Here's an example of an expression which can be evaluated in either context:
my $howmany = @array; # scalar context my @newarray = @array; # list context |
If you look at an array in a scalar context, you'll see how many elements it has; if you look at it in list context, you'll see the contents of the array itself.
Not much, really. A list is just an unnamed array. Here's a demonstration of the difference:
# printing a list of scalars print ("Hello", " ", $name, "\n"); # printing an array @hello = ("Hello", " ", $name, "\n"); print @hello; |
If you come across something that wants a LIST, you can either give it the elements of list as in the first example above, or you can pass it an array by name. If you come across something that wants an ARRAY, you have to actually give it the name of an array.
List values and Arrays are covered on page 47 of the Camel.
Create an array of your friends' names
Print out the first element
Print out the last element
Print out the array from within a double-quoted string using variable interpolation
Print out an array slice of the 2nd to 4th items using variable interpolation
Answers to the above can be found in exercises/answers/arrays.pl
Print the array without putting quotes around its name. What happens?
Set the special variable $, to something appropriate and try the previous step again (see page 132 of your Camel for this variable's documentation)
What happens if you have a small array and then you assign a value to $array[1000]?
Answers to the above can be found in exercises/answers/arrays_advanced.pl