Perl – Arrays vs Lists
Last Updated :
22 Jun, 2020
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.
print ( "List declared and printed: " );
print join ( ' ' , 10, 20, 30, 40, 50);
print ( "\n\n" );
print ( "List declared using qw(): " );
print join ( ' ' , qw(this is gfg) );
print ( "\n\n" );
print ( "Accessing element at index 2: " );
print ((10, 20, 30, 40, 50)[2]);
print ( "\n\n" );
print ( "Range function on list\n" );
print join ( ' ' , 1..6);
print ( "\n\n" );
print ( "Iterating over list elements:\n " );
foreach $element (1..6)
{
print ( "$element\t" );
}
print ( "\n\n" );
print ( "Splicing list\n" );
print ( "Spliced elements: " );
print join ( ' ' , (1..6)[1..3]);
print ( "\n\n" );
|
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.
print ( "complex" , 10, 20, "list" );
|
Output:
complex1020list
Flattened List
If nested lists exist, it is merged to form one single list without any nesting.
print (2, 3, 4, (5, 6));
print ( "\n" );
print (2, 3, 4, 5, 6);
print ( "\n" );
print ((2, 3, 4), 5, 6);
print ( "\n" );
|
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
@array = (10, 20, 30, 40, 50);
print ( "Declared array\n" );
print join ( ' ' , @array );
print ( "\n\n" );
print ( "Accessing element at index 2 \n" );
print ( @array [2]);
print ( "\n\n" );
print ( "Pushing two elements in to the array\n" );
push ( @array , (60, 70));
print join ( ' ' , @array );
print ( "\n\n" );
print ( "Popping elements from array\n" );
print ( "Popped element: " );
print ( pop ( @array ));
print ( "\n" );
print join ( ' ' , @array );
print ( "\n\n" );
print ( "Shift element in an array\n" );
shift ( @array );
print join ( ' ' , @array );
print ( "\n\n" );
print ( "Unshift element in an array\n" );
unshift ( @array , 10);
print join ( ' ' , @array );
print ( "\n\n" );
print ( "Splice an array\n" );
print ( "Spliced elements: " );
print join ( ' ' , splice @array , 3, 2 );
print ( "\nArray after being spliced: " );
print join ( ' ' , @array );
print ( "\n\n" );
print ( "Reverse elements in an array\n" );
print join ( ' ' , reverse @array );
print ( "\n\n" );
print ( "Iterate over elements in an array\n" );
foreach $element ( @array )
{
print ( "$element\t" );
}
print ( "\n\n" );
print ( "Range function\n" );
@array1 = (1..5);
print ( "@array1\t" );
print ( "\n\n" );
|
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.
@array = ( [ 10, 20, 30 ],
[ "ana" , "joe" , "ester" ],
[ "Welcome to gfg" ] );
for $array_ref ( @array )
{
print ( "[ @$array_ref ], \n" );
}
|
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.
Share your thoughts in the comments
Please Login to comment...