VOOZH about

URL: https://www.geeksforgeeks.org/perl/perl-arrays-vs-lists/

⇱ Perl - Arrays vs Lists - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Perl - Arrays vs Lists

Last Updated : 15 Jul, 2025
Perl is a general-purpose, interpreted, dynamic programming languages. Perl has three basic data types namely, scalars, arrays and hashes.

Perl Lists

The list is a sequence of scalar values. However, the list is not a data structure in Perl. There are limited operations that can be performed on the list in Perl. Since no variable refers to this list, lists cannot be used for operations other than printing. Example:
(10, 20, 30);
("this", "is", "a", "list", "in", "perl");

Simple List

A Simple list is one that contains homogeneous elements. Output:
List declared and printed: 10 20 30 40 50

List declared using qw(): this is gfg

Accessing element at index 2: 30

Range function on list
1 2 3 4 5 6

Iterating over list elements:
 1 2 3 4 5 6 

Splicing list
Spliced elements: 2 3 4

Complex List

A complex list is one that contains heterogeneous elements. Output:
complex1020list

Flattened List

If nested lists exist, it is merged to form one single list without any nesting. Output:
23456
23456
23456

Perl Arrays

Array is a Perl data structure. Array in Perl is a variable that contains the list. Array variables are prefixed with '@' sign. Arrays have a wide range of application in Perl. There is no restriction to the type of operation that can be performed on arrays. Arrays in Perl can be 2D but lists cannot be two dimensional. Example:
@num = (10, 20, 30);
@str = ("this", "is", "a", "list", "in", "perl");

Array operations

Output:
Declared array
10 20 30 40 50

Accessing element at index 2 
30

Pushing two elements in to the array
10 20 30 40 50 60 70

Popping elements from array
Popped element: 70
10 20 30 40 50 60

Shift element in an array
20 30 40 50 60

Unshift element in an array
10 20 30 40 50 60

Splice an array
Spliced elements: 40 50
Array after being spliced: 10 20 30 60

Reverse elements in an array
60 30 20 10

Iterate over elements in an array
10 20 30 60 

Range function
1 2 3 4 5 

2D Array

Perl allows the creation of a Two-dimensional array. Output:
[ 10 20 30 ],
[ ana joe ester ],
[ Welcome to gfg ],
Below is a table of differences between Arrays and List:
Based on Arrays Lists
Declaration A variable references a list No variable references
Accessing elements Indexing supported Indexing supported
Range Range supported Range supported
Push Push is supported and newly added element is inserted at the end of the list. Push on list is forbidden.
Pop Pop is supported and an element is popped from the end of the list. Pop is forbidden on list
Loop Array elements can be iterated over using loops List elements can be iterated over using loops
Shift Shift is supported and first element of the array is removed Shift is forbidden on list
Unshift Unshift is supported and adds the element to the front of the array. Unshift is forbidden on list
Splice Splicing is supported in array. Splicing is supported in list.However, the spliced list cannot be accessed as no variable references it.
Reverse Array supports reversal operation List does not support reversal
Often list and array are considered to be same in Perl. But there are differences between the two. While list is not a data structure in Perl, arrays are variables that refer to lists in Perl. In practical applications, we work with arrays.
Comment
Article Tags:

Explore