Learn About Arrays in Perl Programming

Perl (9) - Arrays

Perl (9) - Arrays

Back again to variables. After talking about Scalars, the first type of variables, it is time to see the second one: Arrays. That will be the subject of this article. Enjoy!

What are Arrays?
An array is a collection of scalar values stored together, and referenced using an ordered zero-based index.
In Perl, an array name is any valid identifier, preceded by the @ character.

Defining and Initializing Arrays
Scalar values in an array are surrounded by a pair of parentheses, and separated by commas.

The following statement declares and initializes an array named @countries


This declares the array @names which has four string elements.

Reading and Assigning Individual Array Elements
As I told you when defining what Arrays are, array elements are accessed for reading and writing using their indexes. Also the definition tells us that an array element is a scalar value, so individual array elements are treated as if they were “Scalar variables”, by preceding the array name by dollar sign $ instead of the @ character.

To print the value of the first element of the @countries array:

To assign the value of the 3rd element of the @names array to the variable $n:

To assign the value “USA” to the 5th element of the @countries array:

Obtaining Array Length
An array length is the number of elements in that array. There are two ways to obtain the length of an array:

First: Using the scalar function:

The scalar function returns the length of the array passed to it as argument.

Second: using the Last Index Variable $#array_name
The last index of an array can be determined using the following expression:

The last index is equal to the array length minus one, so the array length can be evaluated as:

Referencing the Last Element
Obviously, it is pretty easy to access the last element in an array using the last index variable. To print the last element in the countries array:

To assign a new value to it:

Another way to access the last elements is using negative indexes:

Array Slicing
You can retrieve a list or range of elements from an array. This operation is called Array Slicing.

Consider the @digits array:

To print the elements from the 3rd to the fifth:

This gives the following output:

To print the first, third, sixth, and eighth elements of the array:

To print the elements from the sixth to the last:

This will print:

Array Functions
Perl has a list of useful built-in functions for manipulating arrays. In this section, we are going to talk about these functions and their uses, and illustrate them by examples.

The push Functions
The push function provides the easiest way to append an element (or more) to the end of an array.


To append the string “China” as a new element at the end of the @countries array:

To append the three names “Mai”, “Salma”, and “Fatma” to the end of the @names array:

If you print the @names array, you should get the following output:

The pop function
The pop function has the opposite effect of the push, i.e. it removes the last element off an array and returns it.


Having the @names array:

The following command:

Will remove the last element “Fatma” out of the @names array, and assigns its value to the variable $n1. If printed, the @names array now looks like this:

Learn the Basics of C Programming Language

The shift Function
The shift function does a similar job to the one that pop function does, with one difference: pop removes and returns the last element of an array, while shift removes and returns the first element.

Given the @countries array with the following elements:

This will remove the first element “Brazil” from the array, and assign its value to the variable $c

The unshift Function
Similar to the push function, but will insert an element at the start of the array.

Given that:

The statement:

will insert the two elements at the start of the array. Printing the @numbers array gives the following output:

The sort Function
As its name implies, the sort function returns a sorted copy of an array. One important point to take into consideration is that the sorting process is non-numeric, i.e. even if the elements to sort are numeric scalars, what gets sorted is their string representation.

The following statement will sort the @countries array and assigns its value to the @sorted array:

Printing the @sorted array will give the following output:

The reverse Function
This function returns a reversed copy of an array.

Starting with the @sorted array from the above example, the following statements will print the elements of the array in reversed order:

In this article, we have talked about Arrays.

  • Arrays are collections of scalar values.
  • Arrays elements are referenced using zero-based index.
  • To access the last elements of an array, the easiest way is to use negative indexes.
  • Perl has a list of functions that insert, remove, sort, and reverse elements of arrays.

This is part one of the Arrays topic, that will be continued by part two. In the next article, we will see the relation between strings and arrays, and how to convert one type to the other. An interesting topic that you shouldn’t miss. See you there.


Please enter your comment!
Please enter your name here