Perl - Arrays vs Lists - GeeksforGeeks

22 Jun, 2020
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.
Example:
(10, 20, 30);
("this", "is", "a", "list", "in", "perl");

A Simple list is one that contains homogeneous elements.
# declaration without variable referencingprint("List declared and printed: ");print join(' ', 10, 20, 30, 40, 50);print("\n\n");  # qw() forms list by extracting words # out of the string using space as a delimiter.print("List declared using qw(): ");print join(' ', qw(this is gfg));print("\n\n");  # indexingprint("Accessing element at index 2: ");print((10, 20, 30, 40, 50)[2]);print("\n\n");  # rangeprint("Range function on list\n");print join(' ', 1..6);print("\n\n");  # loopprint("Iterating over list elements:\n ");foreach $element (1..6){    print("$element\t");}print("\n\n");  # splicingprint("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

A complex list is one that contains heterogeneous elements.
print("complex", 10, 20, "list");
Output:
complex1020list

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
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");

# declaration@array = (10, 20, 30, 40, 50);print("Declared array\n");print join(' ', @array);print("\n\n");  # accessing particular elementprint("Accessing element at index 2 \n");print(@array[2]);print("\n\n");  # pushprint("Pushing two elements in to the array\n");  ## returns total no. of elements in updated arraypush(@array, (60, 70)); print join(' ', @array);print("\n\n");  # popprint("Popping elements from array\n");print("Popped element: ");  ## returns the popped elements of the arrayprint(pop(@array)); print("\n");print join(' ', @array);print("\n\n");  # shiftprint("Shift element in an array\n");shift(@array);print join(' ', @array);print("\n\n");  # unshiftprint("Unshift element in an array\n");unshift(@array, 10);print join(' ', @array);print("\n\n");  # splicing the arrayprint("Splice an array\n");print("Spliced elements: ");print join(' ', splice @array, 3, 2 );print("\nArray after being spliced: ");print join(' ', @array);print("\n\n");  # reversing the arrayprint("Reverse elements in an array\n");print join(' ', reverse @array);print("\n\n");  # loopprint("Iterate over elements in an array\n");foreach $element (@array){    print("$element\t");}print("\n\n");  # rangeprint("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    


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:
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.
Perl-Arrays
perl-list
Picked
Perl
Perl
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Perl | Arrays (push, pop, shift, unshift)
Perl | Polymorphism in OOPs
Perl | Arrays
Perl Tutorial - Learn Perl With Examples
Use of print() and say() in Perl
Perl | length() Function
Perl | join() Function
Perl | Basic Syntax of a Perl Program
Perl | substitution Operator
Perl | Regex Cheat Sheet