Arrays and Lists

List manipulation is a powerful Perl feature for making things simple. A list is a ordered sequence of scalar variables or expressions that evaluate to scalars. The words array and list are often used interchangeably, but by definition, arrays are variables which contain lists. An array variable begins with the '@' symbol. When working with more than one element of a list, enclose them in parentheses.


Array construction

Here are some examples of array construction:

@names = ("Huey",  "Duey", "Louie");
@ages = (3, 11, 9);
@address = (17, "NW Mulberry Street", "Kalamazoo", "MI", 12345);
@empty = ();   # This array contains a null list

Arrays can use the range operator .. to cover all the values from a start value up to and including an end value:

@range_of_numbers = (3, 4, 5, 6, 7, 8, 9, 10); # These two lines are equivalent
@range_of_numbers = (3..10);                   # Uses the range operator


An array can be constructioned from other arrays

@catch_all = (@names, @ages, @address, @empty);

This is equivalent to

@catch_all = ("Huey",  "Duey", "Louie",,3, 11, 9, 17, 
              "NW Mulberry Street", "Kalamazoo", "MI", 12345);


Accessing array elements

To access the elements of an array - which are in fact scalar values - you use an indexed-scalar syntax. Replace the '@' with a '$' and follow the variable name with the index number of the element in braces. The elements of an array are indexed from 0 to the number of elements -1.

@names = ("Huey",  "Duey", "Louie");
@copy_of_names = ($names[0], $names[1], $names[2]);
print $names[0];    # prints Huey
print $names[1];    # prints Duey
print $names[2];    # prints Louie

To find out how many elements are in an array, evaluate the array as a scalar:

$number_of_elements = @names  #  $number_of_elements = 3


Modifiying an Array

Like all good variables, the values stored in an array are not fixed at the time of constuction.

Here we change the value of one element:

$names[1] = "Donald";  # changes the value of the second array element to "Donald"

Here are commands for adding and removing elements from the ends of an array:

push(@my_array, $new_element);   # $new_element is appended to the end of the array
$past_element = pop(@my_array);  # last element of the array is removed and 
                                 # given to the scalar variable $past_element

unshift(@my_array, $new_element);      # $new_element is added to the front of the array
$past_element = shift(@my_array);  # element at the front of the array is removed and
                                     # given to the scalar variable $past_element

And here's a command for changing elements anywhere in an array:

splice(@my_array, $index_in _array_where_replacement_should_begin, 
	   $number_of_existing_elements_to_replace, @replacement_list);


List operations

What makes lists really powerful is how all the elements can be manipulated at once in list functions.

The sort function returns a copy of a list sorted alphabetically

@my_array = ("Huey", "Louie", "Dewey");
@sorted_array = sort(@my_array);         # ("Dewey", "Huey", "Louie")


The reverse function returns a copy of a list with the ordering of the elements reversed;

@reversed_array = reverse(@my_array);    # ("Dewey", "Louie", "Huey")


foreach Loop

A foreach loop is like a for loop that goes through all the elements of list, setting the scalar variable equal to each list element in turn.

foreach $element (@array){
	# $element = current array element
	# perform some operations here
}

Example:

@my_array = ("Huey", "Louie", "Dewey");
foreach $duck (@my_array) {
	print $duck . "\n";
}

Output:

Huey
Louie
Dewey