Perl ProgrammingLearn About Arrays in Perl Programming

Learn About Arrays in Perl Programming

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.

Example
The following statement declares and initializes an array named @countries

@countries = ("Brazil", "Italy", "Germany", "India", "France");

Example

@names = ("Mohamed", "Ali", "Ismail", "Osman");

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:

print ($countries[0]);

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

$n = $names[2];

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

$countries[4] = "USA";

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:
Syntax

scalar(@array_name);

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:

$#array_name;

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

$#array_name + 1;

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:

print ($countries[$#countries]);

To assign a new value to it:

$countries[$#countries] = "South Africa";

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

print ($countries[-1]); # prints the last element
print ($countries[-2]); # prints the element before the last

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

Consider the @digits array:

@digits = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

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

print ("@digits[2..4]);

This gives the following output:

2 3 4

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

0 2 5 7

To print the elements from the sixth to the last:

print ("@digits[6..$#digits]\n");

This will print:

6 7 8 9

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.

Syntax

push (@array_name, newvalue1,…, newvaluen);

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

push (@countries, "China");

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

push (@names, "Mai", "Salma", "Fatma");

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

Mohamed Ali Ismail Osman Mai Salma Fatma

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.

Syntax

$VAR = pop (@array_name);

Example
Having the @names array:

@names = ("Mohamed","Ali","Ismail","Osman","Mai","Salma","Fatma");

The following command:

$n1 = pop (@names);

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:

Mohamed Ali Ismail Osman Mai Salma

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.

Example
Given the @countries array with the following elements:

@countries = ("Brazil", "Italy", "Germany", "India", "France");
$c = shift (@countries);

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:

@numbers = (12, 44, -1, 90);

The statement:

unshift (@numbers,  81, 23);

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

81 23 12 44 -1 90

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.

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

@sorted = sort (@countries);

Printing the @sorted array will give the following output:

Brazil France Germany India Italy

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

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

@rev = reverse (@sorted);
print ("@rev\n");

Summary
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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -