BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Perl – Lists and Arrays

Last Updated: May 15, 2017 by Chaitanya Singh | Filed Under: Perl

In Perl, people use term list and array interchangeably, however there is a difference. The list is the data (ordered collection of scalar values) and the array is a variable that holds the list.

How to define array?

Arrays are prefixed with @ sign. This is how you define an array –

@friends = ("Ajeet", "Chaitanya", "Rahul");

This is the array of strings that holds three strings. Another way of doing the same is:

@friends = qw(Ajeet Chaitanya Rahul); #same as above

Note: qw stands for quoted words, by using qw you can avoid the quote marks and you type less.

While learning Perl, you may come accross few examples, where you see the following kind of array definition:

@friends = qw/Ajeet Chaitanya Rahul/; #same as above

This is because Perl lets you choose any punctuation character as delimiter.

All the following statements are same:

@friends = qw/Ajeet Chaitanya Rahul/;
@friends = qw!Ajeet Chaitanya Rahul!;
@friends = qw;
@friends = qw{Ajeet Chaitanya Rahul};
@friends = qw[Ajeet Chaitanya Rahul];

Note: The opening and closing delimiter must be same.

Access array elements

You must have used arrays in another programming langauge like C, C++, Java etc. The basic concept of array is same here. Lets take an example to understand how to define an array and how to access the elements of it.

#!/usr/bin/perl

@friends = ("Ajeet", "Chaitanya", "Rahul");            

print "\$friends[0] = $friends[0]\n";
print "\$friends[1] = $friends[1]\n";
print "\$friends[2] = $friends[2]\n";

Output:

$friends[0] = Ajeet
$friends[1] = Chaitanya
$friends[2] = Rahul

As you can see in the above program that arrays are prexied with @ symbol. Since, individual array elements are nothing but scalars, they are prexied with $ symbol.

Range operator:

Range operator is denoted by double dots “..”. This operator is used for creating sequential lists. For example:

#!/usr/bin/perl

@num = (3..9);   # same as (3, 4, 5, 6,  7, 8, 9)
foreach $temp (@num) {
   print "$temp\n";
}

Output:

3
4
5
6
7
8
9

Lets take few more examples to understand the range operator:

(2.9..7.9) # same as (2, 3, 4, 5, 6, 7), values after decimal are truncated
(9..3) # empty list, only works in increasing order
(1, 3..6, 10, 12..14) # same as (1, 3, 4, 5, 6, 10, 12, 13, 14),

Operators: pop and push

pop operator removes the last element from an array and returns it. Lets take an example to understand how pop operator works:

#!/usr/bin/perl

@num = (3..7);  # same as (3, 4, 5, 6, 7)
$n1 = pop(@num); # $n1 is 7, array is (3, 4, 5, 6)
$n2 = pop(@num); # $n2 is 6, array is (3, 4, 5)
print "\$n1 is: $n1\n";
print "\$n2 is: $n2\n";
print "array now has:\n";
foreach $temp (@num) {
   print "$temp\n";
}
pop @num; # 5 is discarded, array is (3, 4)
print "array now has:\n";
foreach $temp (@num) {
   print "$temp\n";
}

Output:

$n1 is: 7
$n2 is: 6
array now has:
3
4
5
array now has:
3
4

push operator adds an element to the end of the array.

Example:

#!/usr/bin/perl

@num = (10..12);  # same as (10, 11, 12)
push (@num, 9); # array is (10, 11, 12, 9)
push (@num, 6); # array is (10, 11, 12, 9, 6)
print "array now has:\n";
foreach $temp (@num) {
   print "$temp\n";
}
@num2 = (11, 22, 33);
push (@num, @num2); # adding another array to the end
print "array now has:\n";
foreach $temp (@num) {
   print "$temp\n";
}

Output:

array now has:
10
11
12
9
6
array now has:
10
11
12
9
6
11
22
33

Operators: shift and unshift

As we have seen above that push and pop operators does the adding and removing at the end of the array. The shift and unshift performs operation at the beginning of the array.

shift operator:
shift operator works similar to pop operator, however unlike pop operator it performs the operation at the beginning of the array.

#!/usr/bin/perl

@num = (3..7);  # same as (3, 4, 5, 6, 7)
$n1 = shift(@num); # $n1 is 3, array is (4, 5, 6, 7)
$n2 = shift(@num); # $n2 is 4, array is (5, 6, 7)
print "\$n1 is: $n1\n";
print "\$n2 is: $n2\n";
print "array now has:\n";
foreach $temp (@num) {
   print "$temp\n";
}
shift @num; # 5 is discarded, array is (6, 7)
print "array now has:\n";
foreach $temp (@num) {
   print "$temp\n";
}

Output:

$n1 is: 3
$n2 is: 4
array now has:
5
6
7
array now has:
6
7

unshift operator:
unshift operator works similar to push operator, however unlike push operator it performs the operation at the beginning of the array.

#!/usr/bin/perl

@num = (10..12);  # same as (10, 11, 12)
unshift (@num, 9); # array is (9, 10, 11, 12)
unshift (@num, 6); # array is (6, 9, 10, 11, 12)
print "array now has:\n";
foreach $temp (@num) {
   print "$temp\n";
}
@num2 = (11, 22, 33);
unshift (@num, @num2); # adding another array to the beginning
print "array now has:\n";
foreach $temp (@num) {
   print "$temp\n";
}

output:

array now has:
6
9
10
11
12
array now has:
11
22
33
6
9
10
11
12

splice operator

In the above section we learnt how to perform push, pop, shift and unshift operation on array. However there is a limitation with these operators, they only perform either on the beginning of the array or at the end of the array. What if we want to perform an operation in the middle of the array? thats where splice operator comes into picture.

syntax:

splice @array_name, s, l, @another_array

The splice operator can take upto four arguments.
First argument is array name, here we specify the array on which we are performing operation
second argument is starting point, as I said above, you can perform the opeartion in the middle of the array. This specifies the starting point of the operation.
Third argument is the length
fourth argument is another list or array.

Lets take few examples to understand this:

Example 1: Only two arguments in splice

@myarray = qw(Rahul, Joe, Ajeet, Tim, Lisa);
@myvar = splice @array, 2;  
# removes everything after Ajeet
# @myarray is qw(Rahul, Joe, Ajeet)
# @myvar is qw(Tim, Lisa)

Third and fouth argument of splice operator are optional. In the above example, we have only provided two arguments, array and starting point. Similar to array, the index of splice operator starts at 0, which means Ajeet is the starting point in the above example. If we only provide two arguments then the splice operator removes everything after starting point.

Example 2: Three arguments

The third arguments specifies the length of the removed list of elements. In the above example, we didnt specify any length thats why it removed everything after starting point. Now, lets see what happens when we provide the third argument.

@myarray = qw(Rahul, Joe, Ajeet, Tim, Lisa);
@myvar = splice @array, 2, 1;  
# removes only one element after Ajeet
# @myarray is qw(Rahul, Joe, Ajeet, Lisa)
# @myvar is qw(Tim)

Example 3: Fourth argument

The fourth argument is another list or an array which we are going to insert into our array.

@myarray = qw(Rahul, Joe, Ajeet, Tim, Lisa);
@myvar = splice @array, 2, 1, qw(Harsh, Alisha);  
# removes only one element after Ajeet
# inserts the provided list at the same position
# @myarray is qw(Rahul, Joe, Ajeet, Harsh, Alisha, Lisa)
# @myvar is qw(Tim)

Example 4: What if we do not want to remove anything, just addition

If you do not want to remove anything, you just want to add some elements into the middle of an array then you can specify length as 0.

@myarray = qw(Rahul, Joe, Ajeet, Tim, Lisa);
@myvar = splice @array, 2, 0, qw(Harsh, Alisha); 
# removes nothing
# inserts the provided list at the starting point
# @myarray is qw(Rahul, Joe, Ajeet, Harsh, Alisha, Tim, Lisa)
# @myvar is qw()

The reverse Operator

The reverse operator takes an array of elements (or list) as input and returns it in reverse order.
For example:

@myarray = 10..15;    # same as (10, 11, 12, 13, 14, 15)
@myarray2 = reverse @myarray;   # @myarray2 has (15, 14, 13, 12, 11, 10)
@myarray3 = reverse 5..9; # @myarray3 has (9, 8, 7, 6, 5)

suppose you want to reverse the elements of an array and store it into the same array:

@myarray = 10..15;
@myarray = reverse @myarray;

Note: If you simply write the following statement then it wont work.

reverse @myarray;

This doesn’t do anything as reverse operator doesn’t change the order of elements of an array, it simply returns the list in reverse order that needs to be assigned to an array.

❮ PreviousNext ❯

Top Related Articles:

  1. Data Types in Perl
  2. Perl Variables
  3. Perl – do-while loop with example
  4. Use strict and use warnings in Perl
  5. Scalars in Perl

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Perl Tutorial

  • Perl Tutorial
  • Perl Installation
  • First Perl Program
  • Perl Syntax
  • Data types in Perl
  • Perl Variables
  • my keyword
  • Perl Scalars
  • Use strict and use warnings
  • Perl Arrays
  • Perl Hashes
  • Operators in Perl
  • Perl Conditional statements
  • Perl if
  • Perl if-else
  • Perl if-elsif-else
  • Perl unless
  • Perl unless-else
  • Perl unless-elsif-else
  • Perl switch case
  • Perl given-when-default
  • Perl loops
  • Perl subroutines
  • Perl Strings
  • Perl Escape Sequences

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap