< Perl Tutorial

Perl arrays

← Previous Next →

Perl Arrays

An array is a variable that stores an ordered list of scalar values. Array variables are preceded by an "at" (@) sign. To refer to a single element of an array, we will use the dollar sign ($) with the variable name followed by the index of the element in square brackets. Arrays store values using Lists and is mandatory to understand lists before working with arrays.

A list is a sequence of scalar values enclosed in parentheses. Lists can be as long as needed, and they can contain any scalar value.
An example of a list is as follows:

(1, 5.3, "hello", 2)

We can also create empty lists by simple using two parentheses like " () ". Such lists do not have any element and are called as empty lists.

The following example demonstrates a simple array variable:

@ages = (25, 30, 40);             
@names = ("John Paul", "Lisa", "Kumar");

Array Creation

Perl enables us to store lists in special variables designed for that purpose. These variables are called array variables. Array variables are prefixed with the @ sign and are populated using either parentheses or the qw operator.
An example of Array variable is as follows:

@array = (1, 2, 3);

Here the list containing 1,2,3 is assigned to an array variable named @array.
Another way of creating an array is as follows:

@array = qw/This is an array/;

This leads to a four-element array; the first element is 'this' and last (fourth) is 'array'.

Accessing Array elements

After we have assigned a list to an array variable, we can refer to any element of the array variable as if it is a scalar variable. When accessing individual elements from an array, we must prefix the variable with a dollar sign ($) and then append the element index within the square brackets after the name of the variable.
The following example shows how to access any array element:

@days = qw/Mon Tue Wed Thu Fri Sat Sun/;

print "$days[0]\n";
print "$days[1]\n";
print "$days[2]\n";
print "$days[6]\n";
print "$days[-1]\n";
print "$days[-7]\n";

The above code produces the following output:


Adding and removing Array elements

Perl provides a number of useful functions to add and remove elements in an array. We can use this functions to perform simple tasks such as adding or removing an array element.
The functions are:

  • push @Array, List - Pushes the values of the list onto the end of the array.
  • pop @Array - Pops off and returns the last value of the array.
  • shift @Array - Shifts the first value of the array off and returns it, shortening the array by 1 and moving everything down.
  • unshift @Array, List - Prepends list to the front of the array, and returns the number of elements in the new array.

Following code demonstrates the usage of each of the above defined function:

# create a simple array
@coins = ("Quarter","Dime","Nickel");
print "1. \@coins  = @coins\n";

# add one element at the end of the array
push(@coins, "Penny");
print "2. \@coins  = @coins\n";

# add one element at the beginning of the array
unshift(@coins, "Dollar");
print "3. \@coins  = @coins\n";

# remove one element from the last of the array.
print "4. \@coins  = @coins\n";

# remove one element from the beginning of the array.
print "5. \@coins  = @coins\n";

The output of the above code is as follows:

1. @coins = Quarter Dime Nickel
2. @coins = Quarter Dime Nickel Penny
3. @coins = Dollar Quarter Dime Nickel Penny
4. @coins = Dollar Quarter Dime Nickel
5. @coins = Quarter Dime Nickel
← Previous Next →