Open In App

Perl List and its Types

Introduction to Lists

A list is a collection of scalar values. We can access the elements of a list using indexes. Index starts with 0 (0th index refers to the first element of the list). We use parenthesis and comma operators to construct a list. In Perl, scalar variables start with a $ symbol whereas list variables start with @ symbol.
Important Note : List in perl is not a data structure. They are only some subexpressions/expressions in code. They are typically assigned to an array.
 




#!/usr/bin/perl
 
# Empty List assigned to an array
# Note that the expression () is a
# list and "empty_list" variable is an
# array variable.
@empty_list = ();
 
# Note that the expression (1, 2, 3) is a
# list and "integer_list" variable is an
# array variable.
@integer_list = (1, 2, 3);

  
Lists are of multiple types as described below: 
 






#!/usr/bin/perl
 
# Empty List assigned to an array
@empty_list = ();
 
# List of integers
@integer_list = (1, 2, 3);
 
# List of strings assigned to an array
@string_list = ("Geeks", "for", "Geeks");
 
print "Empty list: @empty_list\n";
print "Integer list: @integer_list\n";
print "String list: @string_list\n";

Empty list: 
Integer list: 1 2 3
String list: Geeks for Geeks




#!/usr/bin/perl
 
# List of strings and integers assigned to an array
@complex_list = (1, 2, "Geeks", "for", "Geeks");
 
# printing this List
print "Complex List: @complex_list";

Complex List: 1 2 Geeks for Geeks




#!/usr/bin/perl
 
# Defining Internal list as an array
@Internal_list = (5, 6, 7);
 
# Defining External list.
@External_list = (1, "Geeks", 3, "For", @Internal_list);
 
# Printing Flattening list
print "Printing list within list: @External_list";

Printing list within list: 1 Geeks 3 For 5 6 7

 

Accessing List Elements

List elements can be accessed with the use of a scalar variable. While accessing a List element, $ is used, because a scalar variable in Perl is accessed with the use of $ symbol. 
Example : 
 






#!/usr/bin/perl
 
# Defining a list
@List = (1, 2, 3, 4, 5);
 
# Accessing list element
print "Second element of List is: $List[2]";

Output: 
 

Second element of List is: 3

 

Slicing a List

Slicing a list in Perl can be done by giving comma(,) separated index values to another list. 
Example: 
 




#!/usr/bin/perl
 
# Defining 1st List
@list1 = (1, "Geeks", 3, "For", 5);
 
# Defining 2nd List
@list2 = @list1[1, 2, 4];
 
# Printing Sliced List
print "Sliced List: @list2";

Output: 
 

Sliced List: Geeks 3 5

 

Defining Range in a List

Range operator in Perl is used as a short way to create a list. When used with list, the range operator simplifies the process of creating a list with contiguous sequences of numbers and letters. The range operator can also be used for slicing the list. 
 

Syntax: leftValue..rightValue 
 

Note: If leftValue is greater than rightValue then it will create an empty list otherwise it will contiguously allocate values from leftValue till rightValue.
Examples : 
 




#!/usr/bin/perl
 
# Defining list with range of a to j
@x = ("a".."j");
 
# Defining list with range of 1 to 15
@y = (1..15);
 
# Defining list with range of A to J
@z = ("A".."J");
 
# Printing these lists
print "List with elements from a to j: @x\n";
print "List with elements from 1 to 15: @y\n";
print "List with elements from A to J: @z";

Output: 
 

List with elements from a to j: a b c d e f g h i j
List with elements from 1 to 15: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
List with elements from A to J: A B C D E F G H I J

Combining Ranges and Slices: 
Range and slice operators can be combined together to perform slicing operation on a list. 
Example: 
 




#!/usr/bin/perl
 
# Defining a list of elements
@x = ("Geeks", 2, 3, "For", 5);
 
# Use of Range and slice operator
@z = @x[2..4];
 
# Printing the sliced List
print "Sliced List: @z";

Output: 
 

Sliced List: 3 For 5

 


Article Tags :