Perl – Arrays vs Lists
Perl is a general-purpose, interpreted, dynamic programming languages. Perl has three basic data types namely, scalars, arrays and hashes.
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.
(10, 20, 30); ("this", "is", "a", "list", "in", "perl");
A Simple list is one that contains homogeneous elements.
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
A complex list is one that contains heterogeneous elements.
If nested lists exist, it is merged to form one single list without any nesting.
23456 23456 23456
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.
@num = (10, 20, 30); @str = ("this", "is", "a", "list", "in", "perl");
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
Perl allows the creation of a Two-dimensional array.
[ 10 20 30 ], [ ana joe ester ], [ Welcome to gfg ],
Below is a table of differences between Arrays and List:
|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.
Please Login to comment...