Close
Close

Array in Perl


In English dictionary, array means collection. In Perl also, it is a collection. But before starting to learn about arrays, first you should know their use. They are very useful for storing data when they are large in number. Imagine that you have to make a software for a firm and they have to store the price of 100 different products. Definitely you need something better than using 100 different variables. And here you can use array. So, let's see this example of declaring an array:

@number = (1,2,3,4,5);
print "@number\n";
Output
1 2 3 4 5

'number' is an array here. We use @ (at) for array. In the next example, 'b' is an empty array.

@b = ();
print "@b\n";
Output

Every element in an array has an index. We can access a particular element using the index of that element. The index of an array starts from 0. This means that the index of the first element will be 0, that of the second element will be 1 and so on. We access a particular element of an array using its index as given below:

$array_name[index]

'array_name' is the name of the array and 'index' is the index of the element which we are trying to access. Since an element of an array is a single element, so we use $ to access it.

element 2 3 15 8 48 13
index 0 1 2 3 4 5
array in Perl

This will be clear by the next example.

@b = (1,2,3,4);
print $b[0],"\n";
print $b[1],"\n";
Output
1
2

As we have seen here, $b[0] gave us the first element i.e. 1. This means that the index of the first element is 0. Similarly, $b[1] gave us 2 and it is the second element.

We can also change the values of an array using their indices. An example of this is given below.

@b = (1,2,3,4);
print $b[0],"\n";
$b[0] = 5;
print $b[0],"\n";
Output
1
5

$b[0] = 5 : Here, $b[0] will simply access the 1st element of the array and $b[0] = 5 will make it 5.

It is not necessary for an array to contain only the same type of elements. We can also have:

@a = (1,2.3,"string");
print "@a\n";
Output
1 2.3 string

So in the above example, the array contains numbers and string also.

Range


.. ( double dot ) is the range operator in Perl. This operator generates a sequence of numbers or characters given to it. (..) is inclusive range operator. This means that (1..5) generates all the numbers from 1 to 5. An example is given below.

@a = (1..5);
@c = ('a'..'e');
print "@a\n";
print "@c\n";
Output
1 2 3 4 5
a b c d e

Size of an array


scalar(@array_name) can be used to obtain the number of elements in an array. scalar(@array_name) is equal to the maximal index which was assigned in the array plus one.

$#array_name is equal to the maximal index itself. It is equal to -1 for an empty array.

Let's see an example to see how these two are used:

@a = (1,2,3,4,5);
print scalar@a,"\n";
print $#a,"\n";
Output
5
4

scalar@array_name or $#array_name gives physical size of an array, not the number of valid elements. The example given below will make it clear.

@a = (3,23,4,3,12,54,65,23,23,45,32,54,23);
$a[29] = 40;
print scalar@a,"\n";
print $#a,"\n";
Output
30
29

As you have seen that @scalar@array_name gave maximal index plus one and one the number of valid elements.

Using while with array


We can use while loop to iterate over all the elements of an array. Let's do this for finding the sum of all the elements of an array.

@a = (3,23,4,3,12,54,65,23,23,45,32,54,23);
$sum = 0;
$i = 0;
while ($i<scalar@a){
  $sum = $sum+$a[$i];
  $i++;
}
print $sum,"\n";
Output
364

scalar@a will give 13. So, 'i' will go up to 12.
$sum = $sum+a[$i] -> initially, 'sum' is 0. Now lets go step by step:
i is 0, sum is 0 and a[0] is 3. So, $sum = $sum + $a[$i] will be sum = 0+3 i.e. 3. Thus the value of sum becomes 3.
Now i is 1, sum is 3 and a[1] = 23. So, sum = 3+23 will make sum 25.
And so on. This will continue up to i = 12.

You can also take the numbers as input from the user using
$a[i] = <>
Try this, take 10 integers of an array from the user and calculate its sum.

Perl has a better way to iterate over the whole array using for loop. You will study this later.

Slicing an array


Slicing means cutting. We can cut an array as per our need using a method called slice. Let's see an example:

slice in Perl
@a = (1,2,3,4,5,6,7,8,9);
@b = @a[1,4];
@c = @a[1,2,3,4];
@d = @a[1..4];
print "@b\n";
print "@c\n";
print "@d\n";
Output
2 5
2 3 4 5
2 3 4 5

@a[1,4] gives an array with elements having indices 1 and 4.
@a[1,2,3,4] gives an array having indices 1, 2, 3, and 4.
@a[1..4] We can also use range operator for slicing an array.

',' operator


comma (,) is used to join or concatenate two arrays. Let's see an example:

@a = ((3,23,4),(4,2,2),12,34,12);
print "@a\n";
Output
3 23 4 4 2 2 12 34 12

As you saw, all the elements of the array were joined by using comma (,).

Useful functions


There are many useful methods which you can use when you need them.

push and pop

push adds an element at the end of an array and pop removes an element from the end of an array.

@a = (1,2,3,4,5,6);
push(@a,7);
print "@a\n";
pop(@a);
print "@a\n";
Output
1 2 3 4 5 6 7
1 2 3 4 5 6

shift and unshift

shift removes an element from the beginning of an array and unshift adds an element at the beginning of an array.
@a = (1,2,3,4,5,6);
unshift(@a,7);
print "@a\n";
shift(@a);
print "@a\n";
Output
7 1 2 3 4 5 6
1 2 3 4 5 6

reverse

The reverse function returns an array which contains the elements of the array passed but in reversed order.

@a = (1,2,3,4,5,6);
@b = reverse(@a);
print "@b\n";
Output
6 5 4 3 2 1

join

join is used to join the elements of an array but by putting something in between them. We write join($value, @array_name) to do this. This will join the elements of the array by putting 'value' in between them. Have a look at the following example:

@a = (1,2,3,4,5,6);
@b = join("\n",@a);
@c = join("a",@a);
print "@b\n@c\n";
Output
1
2
3
4
5
6
1a2a3a4a5a6

map

map is used to transform every element of an array. In this function, each element of the array locally sets to $_. Thus, $_ will represent every element of the array. Let's see an example of adding 2 to every number of an array.

@a = (1,2,3,4,5,6);
@b = map{$_+2} @a;
print "@a\n@b\n";
Output
1 2 3 4 5 6
3 4 5 6 7 8

map{$_+2} @a added 2 to every element the array 'a'. And we have used $_ to access and represent every element of the array.

sort

We can use Perl's sort function to sort the elements of an array. sort(@array_name) will sort every element of the array in ascending order according to their ASCII values. It means that if there is a numeric element in the array, then Perl will treat it as a string and will sort according to the ASCII character and not according to its numeric value. Let's see an example to understand this.

@a = (1,13,2,3,4,5,6);
@b = sort(@a);
@c = ("a","c","e","d");
@d = sort(@c);
print "@b\n@d\n";
Output
1 13 2 3 4 5 6
a c d e

Here, 13 is printed at the second place. This is because Perl treated it as a string i.e. "13" having characters "1" and "3". This is why it came after 1 and before 2.

We can also sort elements of an array i according to their numeric values. This is how we do it:

@a = (1,13,2,3,4,5,6);
#to sort in ascending order
@b = sort {$a <=> $b} @a;
#to sort in descending order
@d = sort {$b <=> $a} @a;
print "@b\n@d\n";
Output
1 2 3 4 5 6 13
13 6 5 4 3 2 1
grep is another useful function which is used on array. It is mentioned in the chapter Regular expressions.

Ask Yours
Post Yours
Doubt? Ask question